When defining a hash_map container, you need to specify not only the type of the key and value, but also the hash and equality functions
(a)the hash function of Hash_map
What exactly is hash< int> like? Look at the source code:
struct hash<int> { operator() (intconstreturn __x;}};
It turns out to be a function object. In SGI STL, the following hash functions are available:
structhash<Char*>structhash<Const Char*>structhash<Char>structhash<unsigned Char>structhash<signed Char>structhash< Short>structhash<unsigned Short>structhash<int>structhash<unsigned int>structhash<Long>structhash<unsigned Long>
In other words, if your key is using one of the above types, you can use the default hash function. Of course you can also define your own hash function. For custom variables, you can only do so, for example, for string, you must customize the hash function. For example:
structstr_hash{size_toperator()(Conststring& str)Const{unsigned Long__h = 0; for(size_t i = 0; i < str.size (); i + +) __h = 5*__h + str[i];returnsize_t (__h); }};//If you wish to take advantage of the system-defined string hash function, you can write:structstr_hash{size_toperator()(Conststring& str)Const{return__stl_hash_string (Str.c_str ()); }};
Be aware of the following points when declaring your own hash function:
- Use a struct, and then overload operator ().
- Return is size_t
- The parameter is the type of key you want to hash.
- The function is of type Const.
(ii) comparison function of Hash_map
In the comparison function in map, the less function needs to be provided. If not provided, the default is less< key>. In Hash_map, you want to compare whether the data in the bucket is equal to the Key, so you need to be equal to the function:equal_to< key>. First look at the source code of EQUAL_TO:
//This code can be from SGI STL///First look at the Binary_function function declaration, but just define some types. Template<class_ARG1,class_ARG2,class_result>structbinary_function {typedef_arg1 First_argument_type;typedef_ARG2 Second_argument_type;typedef_result result_type;};//Look at the definition of equal_to:Template<class_tp>structEqual_to: PublicBINARY_FUNCTION<_TP,_TP,BOOL>{BOOL operator()(Const_tp& __x,Const_tp& __y)Const{return__x = = __y; }};
If you use a custom data type, such as a struct mystruct, or a const char* string, how do I use the comparison function? There are two ways to use a comparison function. The first is: overloading = = operator, using Equal_to; see the following example:
struct mystruct{ int IID; int Len; BOOL operator= = (constconst{ return (iid==my.iid) && (Len==my.len) ; }};
In this way, you can use equal_to< mystruct> as the comparison function. Another way is to use a function object. To customize a comparison function body:
struct compare_str{ booloperator() (constcharconst Charconst{ return strcmp (P1,P2) ==0; }};
With the COMPARE_STR, you can use the hash_map.
typedef hash_map<constchar*, string, hash<constchar*> Compare_str> Strintmap; Strintmap namemap;namemap[" Yeu Bu "]= " Huashan faction head, call the gentleman Sword "; namemap[ "Zhang Sanfeng"]= " Wudang Head, Taijiquan founder "namemap["]= "The first master, Sunflower Treasure book";
(iii) How do I add my own defined type to the hash_map?
#include#include<string>#include<iostream>using namespaceStd//define the classclassclassa{ Public: ClassA (intA): C_a (a) {}intGetValue ()Const{returnC_a;}voidSetValueinta) {c_a;}Private:intC_a;};//1 Define the hash functionstructhash_a{size_toperator()(Const classClassA & A)Const{//Return hash<int> (Classa.getvalue ()); returnA.getvalue (); }};
//2 Define the equal functionstructequal_a{BOOL operator()(Const classClassA & A1,Const classClassA & A2)Const{returnA1.getvalue () = = A2.getvalue (); }};intMain () {Hash_map<classa, String, hash_a, equal_a> hmap; ClassA A1 (12); Hmap[a1]= "I am"; ClassA A2 (198877); Hmap[a2]= "I am 198877"; cout<return0;}
Hash table Hash_map in C + + STL