插入unordered_map<pair<int,int>, int> 抛出编译器错误
Inserting into unordered_map<pair<int,int>, int> throwing compiler errors
以下代码在语义上有什么问题吗?
#include<iostream>
#include<unordered_map>
using namespace std;
int main()
{
unordered_map<pair<int,int>, int> M; // <--- many compiler errors here
pair<int,int> p = make_pair(5,4);
M[p] = 3;
}
我遇到了一堆编译器错误,但对我来说,它似乎应该可以正常工作(如果我只使用 int
作为unordered_map
的键,它就会起作用)。知道为什么会这样吗?
您在以下行收到很多编译器错误:
unordered_map<pair<int,int>, int> M;
这是因为您将pair<int, int>
定义为unordered_map
的键,并且没有pair<int, int>
的隐式hash
(尽管存在operator==
)。
即使您将一些虚拟class X
作为其密钥,也会发生此类错误,如果它没有 hash
和/或operator==
.
一般来说,在声明unordered_map
的键时,应该可以通过"键"访问以下函数:
template <class Key,
class T,
class Hash = hash<Key>, // implement if missing
class Pred = equal_to<Key>, // implement if missing
class Alloc = allocator<pair<const Key,T>>
>
class unordered_map;
在int
的情况下,默认的hash
&operator==
意味着;因此没有错误。
相关文章:
- 为什么在全局范围内使用"extern int a"似乎不行?
- int(c) 和 c-'0' 之间的区别。C++
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 为什么野牛仍在使用"int yylex(void)",却找不到"int yylex(YYS
- 有符号的int和int-有没有一种方法可以在C++中区分它们
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 是否可以从int转换为enum类类型
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 向量 <int> a {N, 0} 和 int arr a[N] = {0} 的时间复杂度有什么区别
- 呼叫运营商<<临时
- 'short int'持有的值溢出,但"自动"不会溢出?
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- 调用'begin(int [n])'没有匹配函数
- 错误:调用"std::vector<:vector<int>>::p ush_back(std::vector<std::__cxx11::basic_string<
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- 如何加入向量&lt; int&gt;到C 中的单个INT
- 是numeric_limits&lt; int&gt; :: is_modulo从逻辑上矛盾
- 运营商&lt;&lt;无法访问班级的私人int
- std::pair的默认构造函数<>将基本类型(int等)设置为零
- 如何显示/打印字符串对象?cout & lt; & lt;Int工作,count <<字符串