当前位置:网站首页>使用 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;
}

边栏推荐
- [applet] realize the left and right [sliding] list through the scroll view component
- Redis master-slave synchronization
- from pip._internal.cli.main import main ModuleNotFoundError: No module named ‘pip‘
- The third part of the construction of the defense system of offensive and defensive exercises is the establishment of a practical security system
- 共享电商的背后: 共创、共生、共享、共富,共赢的共富精神
- Matplotlib常用設置
- [understanding of opportunity-35]: Guiguzi - flying clamp - the art of remote connection, remote control and remote testing
- Is it safe to choose mobile phone for stock trading account opening in Shanghai?
- excel如何打开100万行以上的csv文件
- 2021 RoboCom 世界机器人开发者大赛-高职组初赛
猜你喜欢

What category does the Internet of things application technology major belong to

mt管理器测试滑雪大冒险

云信小课堂 | IM及音视频中常见的认知误区

Why is PHP called hypertext preprocessor

【ES实战】ES上的安全性运行方式

TS初次使用、ts类型

Distance measurement - Hamming distance

from pip._internal.cli.main import main ModuleNotFoundError: No module named ‘pip‘

MT manager test skiing Adventure

2021 RoboCom 世界机器人开发者大赛-本科组初赛
随机推荐
The best smart home open source system in 2022: introduction to Alexa, home assistant and homekit ecosystem
sql 优化
Matplotlib常用图表
边缘计算概述
flutter Unable to load asset: assets/images/888. png
Win 10 mstsc connect RemoteApp
Applet form verification encapsulation
2021 RoboCom 世界机器人开发者大赛-高职组初赛
[swoole Series 1] what will you learn in the world of swoole?
深度学习 | 三个概念:Epoch, Batch, Iteration
有没有一段代码,让你为人类的智慧所折服
VIM color the catalogue
Distance measurement - Hamming distance
使用uni-simple-router,动态传参 TypeError: Cannot convert undefined or null to object
URL introduction
神经网络物联网的未来趋势与发展
SWT / anr problem - SWT causes low memory killer (LMK)
Redis RDB snapshot
2022 crane driver (limited to bridge crane) examination questions and simulation examination
[untitled]