【发布时间】:2018-06-16 12:56:43
【问题描述】:
我想使用 unordered_set 和自定义 struct。在我的例子中,自定义 struct 代表欧几里得平面中的二维点。我知道应该定义一个哈希函数和比较器运算符,我已经这样做了,您可以在下面的代码中看到:
struct Point {
int X;
int Y;
Point() : X(0), Y(0) {};
Point(const int& x, const int& y) : X(x), Y(y) {};
Point(const IPoint& other){
X = other.X;
Y = other.Y;
};
Point& operator=(const Point& other) {
X = other.X;
Y = other.Y;
return *this;
};
bool operator==(const Point& other) {
if (X == other.X && Y == other.Y)
return true;
return false;
};
bool operator<(const Point& other) {
if (X < other.X )
return true;
else if (X == other.X && Y == other.Y)
return true;
return false;
};
size_t operator()(const Point& pointToHash) const {
size_t hash = pointToHash.X + 10 * pointToHash.Y;
return hash;
};
};
但是,如果我按如下方式定义集合,则会出现以下错误:
unordered_set<Point> mySet;
错误 C2280 'std::hash<_kty>::hash(const std::hash<_kty> &)': 试图引用已删除的函数
我错过了什么?
【问题讨论】:
标签: c++ c++11 struct set unordered-set