標籤:his unsigned lib 雜湊 程式 ini closed compare 字元
#include <iostream>#include <unordered_map>using namespace std;class Book{private: string num; string name;public: Book() {}; Book(string num,string name) { this ->name = name;this ->num = num;}; string getNum() { return num;}; string getName() { return name;};};int main(){ unordered_map<string,Book>lib; Book b("001","進階語言程式設計") ; lib[b.getNum()] = b; unordered_map<string,Book>::iterator it; it = lib.find( *(new string("001")) ); if ( it != lib.end() ) cout << (it ->second).getName(); else cout << "沒有給元素!"; return 0;}
unsigned int JSHash(const char *str){ unsigned int hash = 1315423911; while (*str){ hash ^= ((hash << 5) + (*str++) + (hash >> 2)); } return (hash & 0x7FFFFFFF);}class StrHash{public: size_t operator()(const string& s) const { return JSHash(s.c_str()); }};class StrCompare{public: bool operator()(const string& a,const string& b) const { return (a==b); }};字元雜湊及字串比較函數
note:
1.內部實現為: hasht_table
2.樣本中,已string為key,unorder_map已經為string建立了hash值計算函數,所以無需重寫(貌似是這樣的)
3.(Todo)詳解具體使用方法
c++資料結構 --unorder_map