C++11 pair作为无序容器的key

由于 pairpair 没有自带哈希函数,需要手写并传入。无序容器底层为哈希表,需要哈希函数。

1
2
3
4
5
6
7
auto hash_p = [](const pair<int, int> &p) -> size_t {
static hash<long long> hash_ll;
return hash_ll(
p.first + (static_cast<long long>(p.second) << 32)
);
};
unordered_set<pair<int, int>, decltype(hash_p)> lamp_xy(0, hash_p);

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!