C++中有不少中key-value形式的容器,map/hash_map/unordered_map/vector_map。下面讲述各个map的使用及其区别。ios
#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好不少。缘由主要在一下几个方面。函数
可是须要重载<运算符。以下代码所示:性能
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的。spa
固然,你也能够这么作:指针
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;
与上面有相同的效果。code
hash_map,STL中的实现叫作unordered_map,都是基于hash_table实现的。首先,分配一大片内存,造成不少桶。利用hash函数,将key映射到不一样的桶中,固然,也有可能会有两个不一样的key映射到同一个桶中,这是,就须要判别函数来进行查找了。因此,hash_map的key须要两个条件,一个是hash函数,得到映射到的桶的值,另一个是equal_to函数,断定两个key是否相等。显然,当每一个桶里的元素个数比较平均且比较少的时候,查询性能比较高。blog
使用样例以下:排序
#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; }
#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不少桶中可能都没有元素,因此内存利用率不高。内存