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

边栏推荐
- ARP message header format and request flow
- Matplotlib常用設置
- 云信小课堂 | IM及音视频中常见的认知误区
- PostgreSQL notes (10) dynamically execute syntax parsing process
- Li Kou today's question -241 Design priorities for operational expressions
- Leetcode (34) -- find the first and last positions of elements in a sorted array
- Three development trends of enterprise application from the perspective of the third technological revolution
- 股票开户哪个证券公司最好,有安全保障吗
- Redis AOF log
- dat. GUI
猜你喜欢

Redis AOF log

Zero foundation tutorial of Internet of things development

Linux基础 —— CentOS7 离线安装 MySQL

Redis 主从同步

Matplotlib常用图表

mt管理器测试滑雪大冒险

物联网技术应用属于什么专业分类

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

Huisheng Huiying 2022 intelligent, fast and simple video editing software

ARP报文头部格式和请求流程
随机推荐
[micro service sentinel] sentinel integrates openfeign
上海炒股开户选择手机办理安全吗?
Leetcode(34)——在排序数组中查找元素的第一个和最后一个位置
2022 crane driver (limited to bridge crane) examination questions and simulation examination
[swoole Series 1] what will you learn in the world of swoole?
Practical application and extension of plain framework
PostgreSQL source code (58) tuple splicing heap_ form_ Tuple analysis
图的遍历之深度优先搜索和广度优先搜索
What is the difference between memory leak and memory overflow?
Zero foundation tutorial of Internet of things development
2021 RoboCom 世界机器人开发者大赛-高职组初赛
Concepts of dictionary, hash table and array
How to display real-time 2D map after rviz is opened
Notes on problems - /usr/bin/perl is needed by mysql-server-5.1.73-1 glibc23.x86_ sixty-four
每日三题 6.29
小程序表单校验封装
Which securities company is better and which is safer to open a securities account
What is mosaic?
Future trend and development of neural network Internet of things
y53.第三章 Kubernetes从入门到精通 -- ingress(二六)