在C++中使用自定义类型作为std::unordered_map的键时,需提供哈希函数。1. 可通过定义仿函数作为第三个模板参数实现;2. Lambda表达式因类型唯一性限制不推荐直接用于模板;3. 推荐特化std::hash以支持常用自定义类型,使类型能在标准容器中通用;4. 哈希函数应减少冲突,组合成员哈希值并确保类型重载==运算符。

在C++中使用std::unordered_map时,如果键的类型不是内置类型(如int、string),就需要自定义哈希函数。否则编译器会报错,因为标准库不知道如何为自定义类型生成哈希值。
最常见的方式是定义一个函数对象(即重载operator()的结构体或类),作为unordered_map的第三个模板参数。
例如,你想用pair<int int></int>作为键:
#include <unordered_map>
#include <iostream>
<p>struct pair_hash {
size_t operator() (const std::pair<int, int>& p) const {
// 使用异或和位移组合两个整数的哈希
return std::hash<int>{}(p.first) ^ (std::hash<int>{}(p.second) << 1);
}
};</p><p>std::unordered_map<std::pair<int, int>, std::string, pair_hash> my_map;</p><p><span>立即学习</span>“<a href="https://pan.quark.cn/s/6e7abc4abb9f" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">C++免费学习笔记(深入)</a>”;</p>这样就可以正常使用:
my_map[{1, 2}] = "hello";
std::cout << my_map[{1, 2}] << std::endl; // 输出 hello
不能直接把lambda传给模板参数(因为lambda有唯一类型且不能默认构造),但可以用std::function包装,不过效率低,不推荐用于unordered_map模板参数。
更实用的方式是在结构体中定义静态方法或使用decltype配合变量模板(C++14以后):
auto custom_hash = [](const std::pair<int, int>& p) {
return std::hash<int>{}(p.first) ^ (std::hash<int>{}(p.second) << 1);
};
<p>// 需要额外包装,通常不如仿函数方便</p>如果你想让std::hash支持自定义类型,可以在std命名空间中特化std::hash模板。
注意:只能特化用户定义类型,不能特化基础类型如int。
namespace std {
template<>
struct hash<std::pair<int, int>> {
size_t operator()(const std::pair<int, int>& p) const {
return hash<int>{}(p.first) ^ (hash<int>{}(p.second) << 1);
}
};
}
</font>特化后,就可以直接使用默认的unordered_map构造,无需指定第三个参数:
std::unordered_map<std::pair<int, int>, std::string> my_map;
my_map[{3, 4}] = "world";
<< 1是为了减少对称性导致的冲突(如{1,2}和{2,1})return hash<int>{}(p.first) * 31 + hash<int>{}(p.second);
==运算符,因为unordered_map需要判断键是否相等基本上就这些。选择仿函数方式更灵活,特化std::hash更通用。根据你的使用场景决定哪种更适合。
以上就是c++++中如何自定义unordered_map的hash函数_c++ unordered_map自定义hash方法的详细内容,更多请关注php中文网其它相关文章!
c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号