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

边栏推荐
- from pip._internal.cli.main import main ModuleNotFoundError: No module named ‘pip‘
- Distance measurement - Hamming distance
- dat. GUI
- [micro service sentinel] sentinel integrates openfeign
- Redis data types and application scenarios
- ShanDong Multi-University Training #3
- 小程序表单校验封装
- Redis RDB snapshot
- Depth first search and breadth first search of graph traversal
- Current situation and future development trend of Internet of things
猜你喜欢
随机推荐
excel如何打开100万行以上的csv文件
jpa手写sql,用自定义实体类接收
哈工大《信息内容安全》课程知识要点和难点
flutter Unable to load asset: assets/images/888. png
2022 crane driver (limited to bridge crane) examination questions and simulation examination
Postgresql源码(58)元组拼接heap_form_tuple剖析
What are the common types of points mall games?
Redis RDB snapshot
Redis AOF日志
【无标题】
Development trend and future direction of neural network Internet of things
2021 RoboCom 世界机器人开发者大赛-本科组初赛
Applet form verification encapsulation
2022-07-01:某公司年会上,大家要玩一食发奖金游戏,一共有n个员工, 每个员工都有建设积分和捣乱积分, 他们需要排成一队,在队伍最前面的一定是老板
PostgreSQL notes (10) dynamically execute syntax parsing process
Create Ca and issue certificate through go language
图的遍历之深度优先搜索和广度优先搜索
股票开户哪个证券公司最好,有安全保障吗
plain framework的实际应用和扩展
Notes to problems - file /usr/share/mysql/charsets/readme from install of mysql-server-5.1.73-1 glibc23.x86_ 64 c









