#include <iostream>#include<unordered_map>using namespacestd;classbook{Private: stringnum; stringname; Public: Book () {}; Book (stringNumstringName) { This->name = name; This->num =num;}; stringGetnum () {returnnum;}; stringGetName () {returnname;};};intMain () {unordered_map<string,book>Lib; Book B ("001","Advanced Language Programming") ; Lib[b.getnum ()]=b; Unordered_map<string,book>:: Iterator it; It= Lib.find (* (New string("001")) ); if(It! =lib.end ()) cout<< (IT-second). GetName (); Elsecout<<"no element given!"; return 0;}
UnsignedintJshash (Const Char*str) {unsignedinthash =1315423911; while(*str) {Hash^= (Hash <<5) + (*str++) + (hash >>2)); } return(Hash &0x7FFFFFFF);}classstrhash{ Public: size_toperator()(Const string& S)Const { returnJshash (S.c_str ()); }};classstrcompare{ Public: BOOL operator()(Const string& A,Const string& B)Const { return(a==b); }};character hash and string comparison functions
Note
1. The internal implementation is: Hasht_table
2. In the example, string Key,unorder_map has already created a hash value calculation function for string, so there is no need to rewrite it (as it seems)
3. (Todo) Detailed use method
C + + data structure--unorder_map