【发布时间】:2021-03-23 19:02:59
【问题描述】:
#include <bits/stdc++.h>
int main ()
{
std::unordered_map<std::list<int>::iterator, int> map;
return 0;
}
此代码无法编译。错误:
error: no match for call to ‘(const std::hash<std::_List_iterator<int> >) (const std::_List_iterator<int>&)’
noexcept(declval<const _Hash&>()(declval<const _Key&>()))>
我假设由于某种原因我不能使用列表迭代器作为映射键,但是有什么方法可以让它工作吗?我可以将我的设计更改为不需要,但我更喜欢将列表迭代器作为映射键。
【问题讨论】:
-
列表迭代器没有定义散列函数(从错误消息中可以看出)。因此,您应该提供自己的自定义哈希函数。我想这会奏效,虽然有点奇怪。
-
如果你想解决这个错误,你需要为你的密钥类型专门化
std::hash类。
标签: c++ list iterator key unordered-map