如何将unordered_set与自定义结构一起使用?
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>
。
或者,如果您不想指定哈希器,请为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的答案:
- 对于你的
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); };
可以在此处找到更通用的哈希解决方案的想法。
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;
}
- 如何使我的 sizeof sum 结构与空参数包一起工作
- 尝试将指针与结构一起使用时出错
- OOP使用不同的结构与孩子一起构建建议
- 实现具有浮点键的类似哈希表的数据结构,其中公差内的值被合并在一起
- 为什么当我尝试将priority_queue与参数一起使用作为指向结构的指针时会弹出错误
- 如何将 C# 文本框输入与 C 结构和导入一起使用
- 如何将unordered_set与自定义结构一起使用?
- 将 OpenGL VBO 与自定义类/数据结构一起使用
- CMake 项目结构:如何正确地将库合并在一起并将它们包含在多个可执行文件中
- 为什么在 sizeof() 函数中与 * 运算符一起使用和不使用 * 运算符时,指向结构变量的指针大小会有所不同?
- 将 STL 容器与结构一起使用
- 如何修复结构播放器对象中的矢量位置,以便它在 Main 中与 me.position 一起使用?
- 如何与多个内核一起使用模板循环结构
- 有关将智能指针与 Winsock 的 addrinfo 结构一起使用的问题
- 将initalizer列表与从空基类继承的结构一起使用
- 创建自定义迭代器结构以与CSTDIO一起使用
- 将模板气泡排序与结构数组一起使用时出现问题
- 如何与CUDA中的结构内部结构一起工作
- 与圆括号和运算符 () 一起使用的结构名称
- 基于PHP示例,与C 中数据结构一起工作的多态性示例