使用pair作為unordered_map的key


 

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);
        return h1 ^ h2;
    }
};

int main() {
    unordered_map<pair<int, int>, int, pair_hash> table;
    table.emplace(make_pair(1, 1), 1);
    cout << table[make_pair(1, 1)];
    return 0;
}

 

參考鏈接:https://blog.csdn.net/jk_chen_acmer/article/details/98204845


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM