Using C++11 unordered_set in Visual C++ and clang

北城以北 提交于 2019-11-29 02:06:04

In order to make std::unordered_set work with your Point class, you can provide a std::hash specialization for it:

namespace std
{
template<>
struct hash<Point> {
    size_t operator()(const Point &pt) const {
        return std::hash<int>()(pt.x) ^ std::hash<int>()(pt.y);
    }
};
}

You could also change std::unordered_set's second template parameter(it defaults to std::hash<Point>), which indicates a functor type that returns the required hash.

It seems like you tried providing this hash implementation via a user defined conversion to size_t, but that won't work. The fact that it works in VC is caused by some bug in their implementation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!