After I am in the collation will be the type of extraction also placed in this article, I am still full of confusion, often confused,
Template parameters are mainly used in class members of the class is also a template class, when you want to enter only one template parameter, you can also pass the same template parameters to other class members within the class can be used
1 #include <iostream> 2 3 using namespace std; 4 5 6 7 Template<class t> 8 class c7> };12 template < class T, template< class> class Class1 >14 class t b;17 class1&l T T>};
This is a simple implementation of template parameters to implement the class members and the class template using a consistent type method
What is an imitation function, an imitation function is often used in STL libraries
Functor can be defined by the structure of some operator () to implement a similar function of the method, through template parameters can be used, the functor is not a real function, is through the template and struct (or class) implementation, similar to the function of the system, functor can be used in code reuse, Can reduce the redundancy of some of the code, such as the size of the comparison can be integrated into a functor, when used to save a lot of code
1template<classK>2 structHashfuncer3 {4size_toperator()(Constk&key,size_t capacity)5 {6 returnkey%capacity;7 }8 };9 TenTemplate<> One structhashfuncer<string> A { - Staticsize_t Bkdrhash (Const Char*s) - { theUnsignedintSeed =131; -unsigned hash =0; - while(*s) - { +hash = Hash*seed + (*s++); - + } A return(Hash &0x7FFFFFFF);//probably is trying to take a positive number. at } -size_toperator()(Const string&key, size_t _capacity) - { - returnBkdrhash (Key.c_str ())%_capacity; - } - }; in - to + - thetemplate<classKclassVclassHashfun=hashfuncer<k>> * classHashTable $ {Panax Notoginseng};
Here I write the hash table part of the code, here is the use of the functor, the purpose is to deal with different structures, when the hash table is stored in the int type, and when the hash table is a string type, the invocation of the functor is different (I have the template of the functor function), the bottom of the code to invoke the functor function
1 size_t hashfuner (const k& key,size_t capacity)
2 {
3 return hashfun () (key, capacity);
4 }
Because Hashfun is a struct, an anonymous variable is constructed to call the operator () method, which is then encapsulated
It looks like a function, doesn't it?
Template parameters for faux functions and templates