C++

[C++] map에서 key로 pair를 사용할 경우 해쉬값 만들기

코딩준우 2023. 8. 11. 10:55
#include <bits/stdc++.h>


struct hash_pair {
    template <class T1, class T2>
    size_t operator()(const std::pair<T1, T2>& p) const
    {
        size_t hash1 = std::hash<T1>()(p.first);
        auto hash2 = std::hash<T2>{}(p.second);

        if (hash1 != hash2) {
            return hash1 ^ hash2;             
        }
         
        // If hash1 == hash2, their XOR is zero.
        return hash1;
    }
};


int main(int argc, char** argv)
{
    std::unordered_map<std::pair<std::string, std::string>, int, hash_pair> map;
    return 0;
}