使用std::unordered_map中的模板化键
using a templated key in std::unordered_map
我不明白为什么我的编译器不接受低于的代码
#include <unordered_set>
#include <unordered_map>
template<class T>
using M = std::unordered_set<T>;
template<class T>
using D = M<T>;
template<class T>
using DM = std::unordered_map < typename M<T>::const_iterator // Problem
, typename D<T>::const_iterator >; // Problem
int main(int argc, char ** argv)
{
D<int> d;
M<int> m;
DM<int> dm; // Problem
}
编译器命令是
clang++ -std=c++14 test.cpp -o test
编译器错误消息摘录为
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.3.1/../../../../include/c++/5.3.1/bits/hashtable_policy.h:85:11: error:
implicit instantiation of undefined template
'std::hash<std::__detail::_Node_const_iterator<int, true, false> >'
noexcept(declval<const _Hash&>()(declval<const _Key&>()))>
为什么不允许在std::unordered_map
中使用typename M<T>::const_iterator
作为密钥?
因为std::unordered_map
哈希的默认模板参数是std::hash
,它不提供迭代器的实现。
您需要为其提供使用定义的哈希,例如
struct iterator_hash {
template <typename I>
std::size_t operator()(const I &i) const {
return std::hash<int>()(*i); // or return sth based on i
}
};
相关文章:
- 存储在 std::map/std::set 中,与在存储所有数据后对向量进行排序
- 如何在<N>不发生内存泄漏的情况下同时(线程安全)填充 c++11 std::map<std::string,std::bitset*>?
- 无法在 std::map<std::string,std::shared_ptr 中设置值<class>>
- 如何在C++中迭代集合映射(std::map<std::set< char>, int >)?
- 如何初始化结构字段 std::map<std::string, std::string>称为参数
- issue with std::map std::find
- 为什么 std::map< std::map >不释放内存?
- C++ map<std::string> vs map<char *> 性能(我知道,"again?" )
- 确定运行时std::map/std::set的内存使用情况
- 映射上的模板参数无效 std::map< std::string, Stock*> &Stock
- 我的SFINAE检查std::map/std::vector有什么问题
- 使用 std::map<std::string, int> 计算表达式树
- 将数据从两种不同的数据结构插入 std::map <std::string, int> mymap 并通过套接字发送
- 类中的编译器错误,数据类型为 typedef map<std::string,std::p air<std::string,vector<int>>> MapPai
- C++ std::map<std::string, std::set<std::string>> .如何循环设置值?
- 如何填写和访问 std::map<std::p air<enum1, enum2>, funcPtr>?
- 二进制'<':找不到 map<std::string shared_ptr 的运算符<Foo>>
- 如何将 2 个字符* 数组直接映射到 std::map<std::string,std::string>
- C++ std::map<std::string, int> 获取键以特定字符串开头的值
- 从取消引用的迭代器返回 std::map<std::string, int> 时出现巨大的内存泄漏