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

边栏推荐
- Linux foundation - centos7 offline installation of MySQL
- [must] bm41 output the right view of the binary tree [medium +]
- 物联网技术应用属于什么专业分类
- ARP message header format and request flow
- What professional classification does the application of Internet of things technology belong to
- Practical application and extension of plain framework
- PostgreSQL source code (57) why is the performance gap so large in hot update?
- MT manager test skiing Adventure
- Distance measurement - Hamming distance
- 物联网应用技术专业是属于什么类
猜你喜欢

Experience of practical learning of Silicon Valley products

Redis RDB快照

What category does the Internet of things application technology major belong to
![[understanding of opportunity-35]: Guiguzi - flying clamp - the art of remote connection, remote control and remote testing](/img/08/9ecfd53a04e147022dde3449aec132.png)
[understanding of opportunity-35]: Guiguzi - flying clamp - the art of remote connection, remote control and remote testing
![[must] bm41 output the right view of the binary tree [medium +]](/img/a5/00b2f0df5ab448665a2b062d145e52.png)
[must] bm41 output the right view of the binary tree [medium +]

Zhongang Mining: it has inherent advantages to develop the characteristic chemical industry dominated by fluorine chemical industry

2021 robocom world robot developer competition - preliminary competition of undergraduate group

Matplotlib常用设置

2021 RoboCom 世界机器人开发者大赛-高职组复赛

Postgresql源码(57)HOT更新为什么性能差距那么大?
随机推荐
SWT / anr problem - SWT causes low memory killer (LMK)
What professional classification does the application of Internet of things technology belong to
Practical application and extension of plain framework
写给当前及未来博士研究生一些建议整理分享
Yunxin small class | common cognitive misunderstandings in IM and audio and video
【ES实战】ES上的安全性运行方式
2021 RoboCom 世界机器人开发者大赛-本科组初赛
神经网络物联网的未来趋势与发展
从第三次技术革命看企业应用三大开发趋势
Postgresql源码(57)HOT更新为什么性能差距那么大?
Zero foundation tutorial of Internet of things development
Zhao Fuquan: to ensure supply in the short term, we should build a safe, efficient and resilient supply chain in the long term
sql 优化
What is the relationship between modeling and later film and television?
Linux基础 —— CentOS7 离线安装 MySQL
from pip._internal.cli.main import main ModuleNotFoundError: No module named ‘pip‘
dat. GUI
Glass mosaic
Daily three questions 6.30
软件架构的本质