C++ 自定义类作为 unordered_map 和 unordered_set 无序容器的Key
发布时间
阅读量:
阅读量
问题:C++无序容器的使用自定义类作为Key值
#方法: 对类的哈希计算方式及相等性判断函数进行重新编写
#include <iostream>
#include<map>
#include<unordered_map>
#include<unordered_set>
using namespace std;
class A
{
friend size_t hasher(const A& a);
friend bool eqOp(const A& la, const A& ra);
public:
A(const int t,string ss) :a(t),s(ss) {}
private:
int a;
string s;
};
size_t hasher(const A& a)
{
return hash<string>()(a.s);
}
bool eqOp(const A& la, const A& ra)
{
return la.a== ra.a;
}
全部评论 (0)
还没有任何评论哟~
