【C++】unordered_map的哈希HASH重载(转载)

转自 unordered_map的哈希HASH重载——举例unordered_map与pair联合使用_既然弱小,就只顾变强就是了-CSDN博客

如果使用

unordered_map< pair<int, int>, int > mp;

会报错

error: call to implicitly-deleted default constructor of 'std::__1::hash<std::__1::pair<int, int> >'
        : _Hash() {}

这是因为pair还没有HASH键值。

改正需要:

struct hashfunc
{
    template<typename T, typename U>
    size_t operator() (const pair<T, U> &i) const
    {
        return hash<T>()(i.first) ^ hash<U>()(i.second);
    }
};
unordered_map< pair<int, int>, int , hashfunc > mp;

 

posted @ 2021-01-29 23:32  nntzhc  阅读(1690)  评论(0)    收藏  举报