【发布时间】:2020-02-27 13:27:49
【问题描述】:
所以考虑一下这段代码:
#include <bits/stdc++.h>
using namespace std;
int main()
{
unordered_map<vector<pair<int,int>>,int> mp ;
for(int i=0;i<10;i++)
{
vector<pair<int,int>> v ;
for(int j=0;j<10;j++)
v.push_back(make_pair(j,2*j)) ;
mp[v] = i ;
}
return 0;
}
我在这里所做的是创建一个unordered_map,其键类型为vector<pair<int,int>>。如您所见here,这会引发错误。
但是当我将这个 unordered_map 更改为 map、this error doesn't occur anymore 时。
那就是:
#include <bits/stdc++.h>
using namespace std;
int main()
{
map<vector<pair<int,int>>,int> mp ;
for(int i=0;i<10;i++)
{
vector<pair<int,int>> v ;
for(int j=0;j<10;j++)
v.push_back(make_pair(j,2*j)) ;
mp[v] = i ;
}
return 0;
}
效果很好。
那么unordered_maps 并将vectors 作为键放入其中是什么意思?这是怎么回事?
【问题讨论】:
-
85:34: error: no match for call to ‘(const std::hash<std::vector<std::pair<int, int> > >) (const std::vector<std::pair<int, int> >&)这个错误信息还不够清楚吗?只需阅读关键类的要求有map和unordered_map -
这可能会有所帮助:thispointer.com/…。 map 和 unordered map 的区别在于内部处理键的方式。从日志看来,关键向量
> 的散列函数似乎丢失了。 -
你到底为什么要使用整数对的向量作为键?
-
map正在使用运算符<为向量定义排序。unordered_map需要一个哈希值才能将密钥放入适当的存储桶中,但由于您没有指定它,所以它会引发错误。 -
不相关,但可能对未来有所帮助:Why should I not #include<bist/stdc++.h> 和 Why is “using namespace std;” considered bad practice?。也请直接在问题中发布错误消息以节省我们一些时间。
标签: c++ hashmap unordered-map