我一直试图在c++中创建一个结构的unordered_set,但它似乎给了我这个错误-
error: call to implicitly-deleted default constructor
of 'std::__1::hash<coor>'
__compressed_pair_elem(__default_init_tag) {}我包含了一个==运算符,可以帮助我制作一个结构的unordered_set吗?
#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;
}发布于 2021-02-07 01:41:42
std::unordered_set使用散列来唯一地标识对象。它使用std::hash<>结构计算散列。这对于原语或STL容器来说是很好的,因为它们已经为它们定义了一个散列结构。但就您的情况而言,没有用于生成散列的散列结构。错误告诉我们:std::__1::hash<coor>。
为了解决这个问题,我们需要为coor实现一个std::hash<>。
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有了计算散列所需的散列结构。
https://stackoverflow.com/questions/66079773
复制相似问题