使用pair作为unordered_map的key时会提示这样的错误:
The C++ Standard doesn’t provide a hash for this type.
意思是C++标准中没有为pair提供hash函数,所以在使用的时候需要人为传入一个。
pair作为unordered_map的key需要为pair添加hash函数
class ListNode;
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;
}
};
unordered_map<pair<ListNode *, bool>, int> error_hash; //error
unordered_map<pair<ListNode *, bool>, int, pair_hash> ok_hash; //ok