复杂结构作为map的key需要自定义hash

如使用 pair 作为 map 的 key 值。

struct map_key_hash_pair { 
    template <class T1, class T2> 
    size_t operator()(const pair<T1, T2>& a_p) const
    { 
        auto l_hash1 = hash<T1>{}(a_p.first); 
        auto l_hash2 = hash<T2>{}(a_p.second); 
        return l_hash1 ^ l_hash2; 
    } 
};

std::map<std::pair<int, int>, std::string, map_key_hash_pair> l_map;

最后更新于

这有帮助吗?