Template parameters of the imitation function and template, template Template

Source: Internet
Author: User

Template parameters of the imitation function and template, template Template

After finishing the sorting, I will extract the types in this article. I am still full and easy to confuse and often cannot tell clearly,

Template parameters are mainly used when the class members of the class are also template classes. If you want to enter only one template parameter, you can use the same template parameter for other class members in the class at the same time.

 1 #include<iostream> 2  3 using namespace std; 4  5  6  7 template<class T> 8 class class1 9 {10                  T a;11 };12 13 template < class T, template< class> class class1 >14 class class215 {16                  T b;17                  class1<T> c;18 };

This is a simple implementation of template parameters. It is used to implement the method that class members use the same type as the template.

 

 

 

So what is a pseudo-function? A quasi-function is often used in the STL library.

You can define operator () in the struct to implement a function-like method. You can use the parameter passing through the template. The Imitation function is not a real function, implemented through templates and struct (or classes). Similar to the function system, function imitation can be used in code reuse to reduce redundant part of the code, for example, you can integrate the Compare size into a function to save a lot of code when using it.
1 template <class K> 2 struct HashFuncer 3 {4 size_t operator () (const K & key, size_t capacity) 5 {6 return key % capacity; 7} 8 }; 9 10 template <> 11 struct HashFuncer <string> 12 {13 static size_t BKDRHash (const char * s) 14 {15 unsigned int seed = 131; 16 unsigned hash = 0; 17 while (* s) 18 {19 hash = hash * seed + (* s ++); 20 21} 22 return (hash & 0x7FFFFFFF ); // octal is to get a positive value 23} 24 size_t operator () (const string & key, size_t _ capacity) 25 {26 return BKDRHash (key. c_str () % _ capacity; 27} 28}; 29 30 31 32 33 34 template <class K, class V, class HashFun = HashFuncer <K> 35 class HashTable36 {37 };

Some code of the hash table that I wrote is pasted here. Here, the imitation function is used to process different structures. When the hash table is stored in the int type, unlike the string type stored in the hash table, the called function is different (I made a special copy of the template of the function). Below I will post the code for calling the function.

1 size_t HashFuner (const K & key, size_t capacity)

2 {

3 return HashFun () (key, capacity );

4}

Because HashFun is a struct, construct an anonymous variable to call the operator () method and encapsulate it.

This looks like a function, isn't it?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.