[英]How can I use an unordered_set with a custom struct?
我想使用带有自定义struct
的unordered_set
。 就我而言,自定义struct
表示欧几里得平面中的 2D 点。 我知道应该定义一个散列函数和比较器运算符,我已经这样做了,正如您在下面的代码中看到的那样:
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> &)”:试图引用已删除的函数
我错过了什么?
std::unordered_set 的第二个模板参数是用于散列的类型。 并且在您的情况下将默认为std::hash<Point>
,它不存在。 因此std::unordered_set<Point,Point>
如果哈希器的类型相同std::unordered_set<Point,Point>
则可以使用std::unordered_set<Point,Point>
。
或者,如果您不想指定std::hash
为Point
定义std::hash
的Point
并删除成员函数并在专业化的operator()
的主体中实现散列,或从std::hash 专业化。
#include <unordered_set>
struct Point {
int X;
int Y;
Point() : X(0), Y(0) {};
Point(const int& x, const int& y) : X(x), Y(y) {};
Point(const Point& 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) const {
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;
};
// this could be moved in to std::hash<Point>::operator()
size_t operator()(const Point& pointToHash) const noexcept {
size_t hash = pointToHash.X + 10 * pointToHash.Y;
return hash;
};
};
namespace std {
template<> struct hash<Point>
{
std::size_t operator()(const Point& p) const noexcept
{
return p(p);
}
};
}
int main()
{
// no need to specify the hasher if std::hash<Point> exists
std::unordered_set<Point> p;
return 0;
}
虽然上述解决方案可以让您编译代码,但请避免使用点的散列函数。 有一个由b
参数化的一维子空间,其中y = -x/10 + b
线上的所有点都将具有相同的哈希值。 最好使用 64 位散列,其中前 32 位是 x 坐标,低 32 位是 y 坐标(例如)。 那看起来像
uint64_t hash(Point const & p) const noexcept
{
return ((uint64_t)p.X)<<32 | (uint64_t)p.Y;
}
我想通过提供更多提示来扩展rmawatson 的回答:
struct
,您既不需要定义operator=
也不需要定义Point(const Point& other)
,因为您(重新)实现了默认行为。 您可以通过删除if
子句来简化operator==
,如下所示:
bool operator==(const Point& other) { return X == other.X && Y == other.Y; };
您的operator<
存在错误:在else if
子句中,如果两个点相等,则返回true
。 这违反了严格弱排序的要求。 因此,我建议改用以下代码:
bool operator<(const Point& other) { return X < other.X || (X == other.X && Y < other.Y); };
此外,从C++11 开始,您可以使用lambda 表达式而不是定义散列和比较函数。 这样,如果您不需要它们,则不需要为您的struct
指定任何运算符。 将所有内容放在一起,您的代码可以编写如下:
struct Point {
int X, Y;
Point() : X(0), Y(0) {};
Point(const int x, const int y) : X(x), Y(y) {};
};
int main() {
auto hash = [](const Point& p) { return p.X + 10 * p.Y; };
auto equal = [](const Point& p1, const Point& p2) { return p1.X == p2.X && p1.Y == p2.Y; };
std::unordered_set<Point, decltype(hash), decltype(equal)> mySet(8, hash, equal);
return 0;
}
但是,正如CJ13 的回答中所解释的那样,您的哈希函数可能不是最好的。 另一种手工制作散列函数的方法如下:
auto hash = [](const Point& p) { return std::hash<int>()(p.X) * 31 + std::hash<int>()(p.Y); };
可以在此处找到更通用的散列解决方案的想法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.