首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >一组无序的结构

一组无序的结构
EN

Stack Overflow用户
提问于 2021-02-07 01:26:31
回答 2查看 152关注 0票数 0

我一直试图在c++中创建一个结构的unordered_set,但它似乎给了我这个错误-

代码语言:javascript
复制
error: call to implicitly-deleted default constructor
      of 'std::__1::hash<coor>'
  __compressed_pair_elem(__default_init_tag) {}

我包含了一个==运算符,可以帮助我制作一个结构的unordered_set吗?

代码语言:javascript
复制
#include <iostream>
#include <vector>
#include <unordered_set>
#include <algorithm>
using namespace std;
struct coor{
    int x,y;
    bool operator==(coor a) const{
        if(a.x == x && a.y == y){
            return true;
        }
        else return false;
    };
};
int main(){
    unordered_set<coor> myset;
}
EN

Stack Overflow用户

发布于 2021-02-07 01:41:42

std::unordered_set使用散列来唯一地标识对象。它使用std::hash<>结构计算散列。这对于原语或STL容器来说是很好的,因为它们已经为它们定义了一个散列结构。但就您的情况而言,没有用于生成散列的散列结构。错误告诉我们:std::__1::hash<coor>

为了解决这个问题,我们需要为coor实现一个std::hash<>

代码语言:javascript
复制
namespace std {
    template<>
    struct hash<coor> {
        const size_t operator()(const coor& c) const
        {
            return std::hash<int>()(c.x) ^ std::hash<int>()(c.y);   
        }
    };
}

现在,std::unordered_set有了计算散列所需的散列结构。

票数 1
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66079773

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档