Hash for a std::pair, for use in an unordered_map
Hash for a std::pair, for use in an unordered_map
当做
unordered_map<pair<unsigned int, unsigned int>, unsigned int> m;
我们得到
错误 C2338:C++标准不为此类型提供哈希。
是否有一种内置方法来定义int
std::pair
哈希,或者我们需要手动定义它?(在这种情况下,哈希可能只是(第一项的字节((对中第二项的字节(粘在一起(。
注意:我使用的是VC++ 2013。
注意2:pair<int,int>pair作为unordered_map问题的键的答案并没有清楚地解决如何实际创建具有两个int
哈希的问题,详见此处。
如果你不想使用boost,那么滚动你自己的不应该太难。 添加static_assert以确保保持 2 个整数适合 1 个size_t的假设。
using IntPair = std::pair<int, int>;
struct IntPairHash {
static_assert(sizeof(int) * 2 == sizeof(size_t));
size_t operator()(IntPair p) const noexcept {
return size_t(p.first) << 32 | p.second;
}
};
std::unordered_map<IntPair, int, IntPairHash> myMap;
相关文章:
- "error: no matching function for call to"构造函数错误
- 表示"accepting anything for this template argument" C++概念的通配符
- 如何在C++中从两个单独的for循环中添加两个数组
- 在Linux for Windows上编译C++代码时出错
- 调用专用模板时出错"no matching function for call to [...]"
- 为什么我的for循环不能正确获取argv
- 为什么我不能在 FOR LOOP 中使用 i/10,C++?
- Arduino:for/while/if在void setup()或void loop()之前?——错误:之前需要不合格
- 在基于范围的for循环中使用结构化绑定声明
- 通过for循环使用用户输入填充列表
- 使用for循环检查数组中的重复项
- 在for循环中使用auto vs decltype(vec.size())来处理字符串的向量
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 正在使用for循环创建QScatterSerie
- Python中的for循环与C++有何不同
- std::memory_order for std::atomic:<T>:wait
- 在更改for循环的第三部分后,未使用for循环结果
- 在 for 循环中查找问题时遇到困难
- 嵌套for循环C++的问题(初学者)
- 如何用for循环在c++中生成单词三角形