如何将unordered_set与自定义结构一起使用?

How can I use an unordered_set with a custom struct?

本文关键字:一起 结构 自定义 unordered set      更新时间:2023-10-16

我想使用带有自定义structunordered_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>

或者,如果您不想指定哈希器,请为Point定义std::hash的专用化,然后摆脱成员函数并在专用化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;
}

演示

我想通过提供更多提示来扩展rmawatson的答案:

  1. 对于你的struct,你既不需要定义operator=,也不需要定义Point(const Point& other),因为你(重新(实现了默认行为。
  2. 您可以通过删除if子句来简化operator==,如下所示:

    bool operator==(const Point& other) { return X == other.X && Y == other.Y; };
    
  3. 您的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); };

可以在此处找到更通用的哈希解决方案的想法。

Ideone上的代码

虽然上述解决方案可以让您编译代码,但请避免使用散列函数来表示点数。有一个由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;
}