因为unordered_map内部是由哈希表实现的,哈希表中的每一项为一个桶,分别装着键和值,原stl中不包含pair<int,int>
类型的键,所以需要自定义
#include <iostream>
#include <unordered_map>
// 分别计算出内置类型的 Hash Value 然后对它们进行 Combine 得到一个哈希值
// 一般直接采用移位加异或(XOR)得到哈希值
struct HashFunc
{
template<typename T, typename U>
size_t operator()(const std::pair<T, U>& p) const {
return std::hash<T>()(p.first) ^ std::hash<U>()(p.second);
}
};
// 键值比较,哈希碰撞的比较定义,需要直到两个自定义对象是否相等
struct EqualKey {
template<typename T, typename U>
bool operator ()(const std::pair<T, U>& p1, const std::pair<T, U>& p2) const {
return p1.first == p2.first && p1.second == p2.second;
}
};
int main()
{
std::unordered_map<std::pair<int, int>, int, HashFunc, EqualKey> mmap;
mmap[{
1, 3}] = 2;
mmap[{
2, 3}] = 4;
mmap[{
3, 3}] = 7;
mmap[{
5, 3}] = 6;
mmap[{
1, 1}] = 1;
int a, b;
while (std::cin >> a >> b) {
if (mmap.find({
a, b }) != mmap.end()) {
std::cout << mmap[{
a, b}] << "\n";
}
}
std::system("pause");
}