C++关联容器之unordered-map
-
主要着眼于unordered_map的基本操作,会用即可。
-
unordered_map是C++中的哈希表,可以在任意类型和任意类型间做映射。
-
基本操作如下:
-
使用需要
#include<unordered_map>
-
定义一个hashtable,
unordered_map<int,int>,
unordered_map<string,double>
...... -
插入:例如将
("ABC" -> 5.45)
插入unordered_map<string, double> hash
中,hash["ABC"]=5.45
-
查询:
hash["ABC"]
会返回5.45 -
判断key是否存在:
hash.count("ABC") != 0
或hash.find("ABC") != hash.end()
-
遍历
for(auto &c : hash)cout<<c.first<<" "<<c.second<<endl; //法1 for(auto &[k,v] : hash)cout<<k<<" "<<v<<endl; //法2
-
如果想让自定义的class作为key(
unordered_map<key,value>
)来使用unordered_map,需要实现:-
哈希函数,需要实现一个class重载
operator()
,将自定义class变量映射到一个size_t
类型的数。一般常用std::hash
模板来实现。 -
判断两个自定义class类型的变量是否相等的函数,一般在自定义class里重载
operator==
。 -
示例代码
#include <iostream> #include <vector> #include <unordered_map> using namespace std; class Myclass { public: int first; vector<int> second; // 重载等号,判断两个Myclass类型的变量是否相等 bool operator== (const Myclass &other) const { return first == other.first && second == other.second; } }; // 实现Myclass类的hash函数 namespace std { template <> struct hash<Myclass> { size_t operator()(const Myclass &k) const { int h = k.first; for (auto x : k.second) { h ^= x; } return h; } }; } int main() { unordered_map<Myclass, double> S; Myclass a = { 2, {3, 4} }; Myclass b = { 3, {1, 2, 3, 4} }; S[a] = 2.5; S[b] = 3.123; cout << S[a] << ' ' << S[b] << endl; return 0; }
-
-