当前位置:网站首页>使用 pair 做 unordered_map 的键值
使用 pair 做 unordered_map 的键值
2022-07-01 23:25:00 【litanyuan】
背景
标准库中 unordered_map 是使用哈希表实现的,对于内置类型标准库通过 std::hash 提供了哈希函数的实现,因此若采用非内置类型做键值,则需要程序员自己提供其哈希函数的实现。
用 pair 做键值
①.自定义哈希函数
struct pairHash
{
template<typename T,typename U>
size_t operator()(const pair<T, U> & p) const
{
return hash<T>()(p.first) ^ hash<U>()(p.second);
}
template<typename T, typename U>
bool operator() (const pair<T, U> & p1, const pair<T, U> & p2) const
{
return p1.first == p2.first && p1.second == p2.second;
}
};
②.代码示例
unordered_map<pair<int, int>, int, pairHash, pairHash> m;
m[{
2, 4}] = 24;
m[{
4, 2}] = 42;
cout << m[{
2, 4}] << endl;
cout << m[{
4, 2}] << endl;
万能哈希函数
①.概述
对于任意自定义类型,我们只需要提供其哈希函数即可作为 unordered_map 的 key 使用。
②.万能哈希函数
template <typename... Types>
size_t hash_val(const Types&... args)const
{
size_t seed = 0;
hash_value(seed, args...);
return seed;
}
template <typename T, typename... Types>
void hash_value(size_t& seed,const T& firstArg,const Types&... args) const
{
hash_combine(seed, firstArg);
hash_value(seed, args...);
}
template <typename T>
void hash_value(size_t& seed,const T& val) const
{
hash_combine(seed, val);
}
template<typename T>
void hash_combine(size_t& seed,const T& val) const
{
seed ^= std::hash<T>()(val) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
③.代码示例
struct demoStruct
{
string name;
int id;
demoStruct( const string & m_name ,int m_id):name(m_name),id(m_id){
}
bool operator == (const demoStruct &demo) const
{
return demo.id == id && demo.name == name;
}
};
class CustomHashDemo
{
public:
std::size_t operator()(const demoStruct& c) const
{
return hash_val(c.name, c.id);
}
private:
/*同上*/
};
int main()
{
unordered_map<demoStruct,int, CustomHashDemo> m;
m[{
"张三", 12}] = 24;
m[{
"李四", 42}] = 42;
return 0;
}
边栏推荐
- 2022 crane driver (limited to bridge crane) examination questions and simulation examination
- Postgresql源码(58)元组拼接heap_form_tuple剖析
- Redis AOF log
- 深度学习 | 三个概念:Epoch, Batch, Iteration
- Practical application and extension of plain framework
- 物联网技术应用属于什么专业分类
- Material Design组件 - 使用BottomSheet展现扩展内容(一)
- PostgreSQL source code (57) why is the performance gap so large in hot update?
- 常见的积分商城游戏类型有哪些?
- 每日三题 6.29
猜你喜欢
[micro service sentinel] sentinel integrates openfeign
Redis data types and application scenarios
2022 examination questions and online simulation examination for safety management personnel of hazardous chemical business units
Why is PHP called hypertext preprocessor
为什么PHP叫超文本预处理器
How to display real-time 2D map after rviz is opened
2021 robocom world robot developer competition - semi finals of higher vocational group
Redis AOF log
Material Design组件 - 使用BottomSheet展现扩展内容(一)
2022年最佳智能家居开源系统:Alexa、Home Assistant、HomeKit生态系统介绍
随机推荐
Is it safe to choose mobile phone for stock trading account opening in Shanghai?
from pip._internal.cli.main import main ModuleNotFoundError: No module named ‘pip‘
Daily three questions 6.28
Three development trends of enterprise application from the perspective of the third technological revolution
问题随记 —— /usr/bin/perl is needed by MySQL-server-5.1.73-1.glibc23.x86_64
Li Kou today's question -241 Design priorities for operational expressions
from pip._ internal. cli. main import main ModuleNotFoundError: No module named ‘pip‘
[leetcode] length of the last word [58]
【无标题】
Switch to software testing, knowing these four points is enough!
dat. GUI
常见的积分商城游戏类型有哪些?
使用uni-simple-router,动态传参 TypeError: Cannot convert undefined or null to object
上海炒股开户选择手机办理安全吗?
Practical application and extension of plain framework
excel如何打开100万行以上的csv文件
Leetcode (34) -- find the first and last positions of elements in a sorted array
Know --matplotlib
What is mosaic?
Who do you want to know when opening a stock account? Is it safe to open an account online?