pair<int,int> pair as key of unordered_map issue

限于喜欢 提交于 2019-11-27 13:37:56

This happens because there is no specialization for std::tr1::hash<Key> with Key = std::pair<int, int>. You must to specialize std::tr1::hash<Key> with Key = std::pair<int, int> before declaring tr1::unordered_map<Pair,bool> h;. This happens because std don't know how to hash a pair<int, int>.

Following there is a example of how to specialize std::tr1::hash<>

template <>
struct std::tr1::hash<std::pair<int, int> > {
public:
        size_t operator()(std::pair<int, int> x) const throw() {
             size_t h = SOMETHING;//something with x   
             return h;
        }
};
Manohar Reddy Poreddy

Ran into the same issue:

unordered_map <pair<x, y>, z> m1;

A few workarounds are:

unordered_map <stringxy, z> m1;
// the first and second of the pair merged to a string
//   though string parsing may be required, looks same complexity overall

unordered_multimap <x, pair<y, z>> m1;
// second of the pair of the key went into value.  
//   time complexity slightly increases

deque<deque<x>> d1;
// here x & y are of same type, z is stored as: d1[x][y] = z
//   space required is x * y, however time complexity is O(1)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!