C++中有很多中key-value形式的容器,map/hash_map/unordered_map/vector_map。下面講述各個map的使用及其區別。
map:
#include <iostream> #include <map> using namespace std; typedef std::map<int, string> Map; typedef Map::iterator MapIt; int main() { Map *map = new Map(); int key; string value; while(cin>>key>>value) { map->insert(make_pair(key, value)); } for(MapIt it = map->begin(); it != map->end(); ++it) cout<<"key:"<<it->first<<" value:"<<it->second<<endl; delete map; return 0; }
map使用紅黑樹實現。查找時間在O(lg(n))-O(2*log(n))之間,構建map花費的時間比較長,因而,map使用於那種插入和查詢混合的情況。如果是先插入后查詢的情況,可以考慮使用vector_map.
vector_map在C++中沒有實現,想使用可以自己實現。其基本思想在於使用vector來保存數據,插入完成后進行排序,然后使用而分查找進行查詢。這樣在先插入后查詢的條件下,性能會比map好很多。原因主要在一下幾個方面。
- vector使用線性存儲,map是二叉樹形狀,所以vector的局部性更好。
- vector可以一次分配很大的內存,而map需要每次分配一個節點,而且map中相對於vector有很多冗余數據,比如指向子節點的指針。
- vector是插入完成后統一進行排序,而map每次insert都有一次查找和樹的旋轉。
- vector_map是二分查找,查找時間穩定在O(lg(n)),而map的存儲結構是紅黑樹,查找時間為O(lg(n))-O(2*log(n))。
map的key可以是自定義數據結構,
但是需要重載<運算符。如下代碼所示:
typedef struct _Key { _Key(int *p, int l) { len_ = l; for(int i = 0; i < l; ++i) p_[i] = p[i]; } bool operator<(const _Key &rs) const { if(len_ == rs.len_) { for(int i = 0; i < len_; ++i) return p_[i] < rs.p_[i]; return false; } else return len_ < rs.len_; } int p_[MaxLen]; int len_; }Key; typedef std::map<Key, vector<int> *> MyMap;
需要注意的是,重載函數必須為const的。
當然,你也可以這么做:
typedef struct _Key { _Key(int *p, int l) { len_ = l; for(int i = 0; i < l; ++i) p_[i] = p[i]; } int p_[MaxLen]; int len_; }Key; typedef struct _KeyCmp { bool operator()(const Key &ls, const Key &rs) { if(ls.len_ == rs.len_) { for(int i = 0; i < ls.len_; ++i) return ls.p_[i] < rs.p_[i]; return false; } else return ls.len_ < rs.len_; } }KeyCmp; typedef std::map<Key, vector<int> *, KeyCmp> MyMap;
與上面有相同的效果。
hash_map
hash_map,STL中的實現叫做unordered_map,都是基於hash_table實現的。首先,分配一大片內存,形成很多桶。利用hash函數,將key映射到不同的桶中,當然,也有可能會有兩個不同的key映射到同一個桶中,這是,就需要判別函數來進行查找了。所以,hash_map的key需要兩個條件,一個是hash函數,獲得映射到的桶的值,另外一個是equal_to函數,判定兩個key是否相等。顯然,當每個桶里的元素個數比較平均且比較少的時候,查詢性能比較高。
使用樣例如下:
#include <string> #include <iostream> #include <ext/hash_map> using namespace std; using namespace __gnu_cxx; struct str_hash { size_t operator()(const string &s) const { return __stl_hash_string(s.c_str()); } }; struct str_compare { int operator()(const string &a, const string &b) const { return (a==b); } }; typedef hash_map<string, string, str_hash, str_compare> StrMap; int main() { StrMap strMap; string a,b; cout<<"插入:"<<endl; while(cin>>a>>b) { if(a.length() <= 1) break; strMap.insert(make_pair(a,b)); } cout<<"查詢:"<<endl; while(cin>>a) { if(a.length() <= 1) break; if(strMap.find(a) != strMap.end()) cout<<strMap[a]<<endl; else cout<<"not found"<<endl; } return 0; }
unordered_map
#include <iostream> #include <unordered_map> #include <string> using namespace std; unsigned int JSHash(const char *str) { unsigned int hash = 1315423911; while(*str) { hash ^= ((hash<< 5) + (*str++) + (hash>>2)); } return (hash & 0x7FFFFFFF); } struct StrHash { size_t operator()(const string &s) const { return JSHash(s.c_str()); } }; struct StrCompare { bool operator()(const string &a, const string &b) const { return a==b; } }; typedef unordered_map<string, string, StrHash, StrCompare> MyMap; int main() { MyMap mymap; string a,b; while(cin>>a>>b) { mymap[a] = b; } for(MyMap::iterator it = mymap.begin(); it != mymap.end(); ++it) cout<<it->first<<" "<<it->second<<endl; return 0; }
總體來說,hash_map的查找速度比map要快,因為hash_map的查找速度與數據量大小無關,屬於常數級別。map的查找速度是log(n)級別。但是hash_map每次查找都需要執行hash函數,所以也比較耗時。而且,hash_map很多桶中可能都沒有元素,所以內存利用率不高。