【发布时间】:2013-08-08 12:31:56
【问题描述】:
我正在尝试像这样定义一个 unordered_set:
unordered_set<Point> m_Points;
当我编译它时,我得到以下错误:
C++ 标准不提供这种类型的散列。
班级Point:
class Point{
private:
int x, y;
public:
Point(int a_x, int a_y)
: x(a_x), y(a_y)
{}
~Point(){}
int getX()const { return x; }
int getY()const { return y; }
bool operator == (const Point& rhs) const{
return x == rhs.x && y == rhs.y;
}
bool operator != (const Point& rhs) const{
return !(*this == rhs);
}
};
- 如何/在哪里为 Point 定义哈希函数?
- 对于二维点,什么是好的散列函数?
【问题讨论】:
-
有一个很好的描述here。
-
一个懒惰的解决方案可能是从
std::pair<int, int>派生你的类... -
我可以在 Point 类中定义一个哈希函数并将其作为函子传递给模板的第二个参数中的 unordered_set 吗?
-
@brainydexter 是的 - 第二个参数是类 Hash 默认为 std::hash
-
@doctorlove 是的..我想弄清楚的是,如果我在 Point 类中指定一个哈希函数,当我定义 unordered_set 时,如何将它指定为函子?我不确定我是否能做到。
标签: c++ stl set unordered-set