遅いのでそもそもpairをkeyに使わない方がいい
例:座標をキーとして使うなら”make_pair<y, x>”をキーにするんじゃなくて”y * W + x”をキーにする
struct pair_hash {
template <class T1, class T2>
std::size_t operator() (const std::pair<T1, T2>& p) const {
auto h1 = std::hash<T1>{}(p.first);
auto h2 = std::hash<T2>{}(p.second);
// Combine the two hashes (example taken from boost::hash_combine)
// Another ref: <https://github.com/HowardHinnant/hash_append/issues/7>
if (sizeof(T1) > 8)
return h1 ^ (h2 + 0x9e3779b97f4a7c15ULL + (h1 << 12) + (h1 >> 4));
else
return h1 ^ (h2 + 0x9e3779b9 + (h1 << 6) + (h1 >> 2));
}
};
Example: unordered_map<std::pair<int, int>, int, pair_hash> ump; Example: unordered_map<std::pair<int, int>, pair_hash> ump;