如何将unordered

编程入门 行业动态 更新时间:2024-10-24 14:25:49
本文介绍了如何将unordered_set与自定义结构一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想使用 unordered_set 和自定义 struct 。在我的情况下,自定义 struct 表示欧氏平面中的2D点。我知道应该定义一个散列函数和比较器运算符,并且已经做到了,正如您在下面的代码中看到的那样:

I want to use an unordered_set with a custom struct. In my case, the custom struct represents a 2D point in an euclidean plane. I know that one should define a hash function and comparator operator and I have done so as you can see in my code below:

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; }; };

但是,如果我将集合定义如下,则会出现以下错误:

However, I'm getting the error below, if I define the set as follows:

unordered_set<Point> mySet;

错误C2280'std :: hash< __ Kty> :: hash( const std :: hash< _Kty>&)':尝试引用已删除的函数

Error C2280 'std::hash<_Kty>::hash(const std::hash<_Kty> &)': attempting to reference a deleted function

我缺少什么?

推荐答案

std :: unordered_set的第二个模板参数是用于哈希的类型。并且在您的情况下默认为 std :: hash< Point> (不存在)。因此,如果散列器类型相同,则可以使用 std :: unordered_set< Point,Point> 。

The second template parameter to std::unordered_set is the type to use for hashing. and will default to std::hash<Point> in your case, which doesn't exist. So you can use std::unordered_set<Point,Point> if the hasher is the same type.

如果不想指定散列器,则为 Point 定义 std :: hash 的特化,然后除掉成员函数,并在您的专长的 operator()主体中实现散列,或从std :: hash专长调用该成员函数。

Alternatively if you do not want to specify the hasher, define a specialization of std::hash for Point and either get rid of the member function and implement the hashing in the body of your specialization's operator(), or call the member function from the std::hash specialization.

#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; }

演示

更多推荐

如何将unordered

本文发布于:2023-10-24 19:37:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524815.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何将   unordered

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!