📄 string_hash.h
字号:
#include <ext/hash_map>#include <string>#include <iostream>using namespace std;using namespace __gnu_cxx;struct eqstr{ bool operator()(const string s1, const string s2) const { return strcmp(s1.c_str(), s2.c_str()) == 0; }};namespace __gnu_cxx { template <> struct hash<const string> { size_t operator()(const string& s) const { return hash<char const*>()(s.c_str()); } };};typedef hash_map<const string,unsigned int,hash<const string>,eqstr> HASHM;typedef HASHM::iterator HASHMI; // const doesn't change behaviorclass stringHash{public: void dumpAllValues(){ cout << "dump 1:" << endl; // list all content for (HASHMI i=h.begin();i!=h.end();i++) cout<<i->first<<" "<<i->second<<endl; } void erase(HASHM::iterator it){ h.erase(it); } void insert(string s, int n){ h.insert( make_pair(s, n) ) ;} unsigned int get(string s) { return h[s]; } void increment(string s) { h[s]++; } inline HASHMI begin() { return h.begin(); } inline HASHMI end() { return h.end(); } private: HASHM h; string n; /* n="hello"; h[n]=20; n="goodbye"; h[n]=10; */ /* n="yesterday"; h.insert( make_pair(n, 30) ); */ /* n="today"; h.insert( make_pair(n.c_str(), 40) ); */ /* // Let's erase some items: */ /* h.erase(h.begin()); // Erase whichever element is first */ /* h.erase("hello"); // Erase a specific element */ /* cout << "dump 2:" << endl; // list all content again */ /* for(HASHMI i=h.begin();i!=h.end();i++) cout<<i->first<<" "<<i->second<<endl; */ /* // h.clear(); // Works fine */ /* // Loop to delete specific elements works OK. */ /* HASHMI i=h.begin(); */ /* while (! h.empty()) { */ /* // h.erase((*i).first); // works */ /* h.erase(h.begin()); // works */ /* // h.erase(i); // won't compile */ /* i=h.begin(); */ /* } */ /* // Loop to delete all elements one at a time will NOT work, */ /* // regardless of iterator type. Elements will delete OK, but */ /* // the loop either doesn't finish or segfaults */ /* for(HASHMI i=h.begin();i!=h.end();++i) { */ /* // h.erase(i); // won't compile */ /* // h.erase(h.begin()); // segfaults */ /* // h.erase((*i).first);// segfaults */ /* } cout << "size at end=" << h.size() << endl; return(0); */ /* } */};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -