当前位置:网站首页>Use pair to do unordered_ Key value of map
Use pair to do unordered_ Key value of map
2022-07-01 23:42:00 【litanyuan】
background
Standard library unordered_map It is implemented using hash table , For the built-in type standard library, pass std::hash Provides the implementation of hash function , Therefore, if non built-in types are used as key values , The programmer needs to provide the implementation of its hash function .
use pair Make key value
①. Custom hash function
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;
}
};
②. Code example
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;

Universal hash function
①. summary
For any custom type , We only need to provide its hash function as unordered_map Of key Use .
②. Universal hash function
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);
}
③. Code example
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:
/* ditto */
};
int main()
{
unordered_map<demoStruct,int, CustomHashDemo> m;
m[{
" Zhang San ", 12}] = 24;
m[{
" Li Si ", 42}] = 42;
return 0;
}

边栏推荐
- golang中的iota
- 哈工大《信息内容安全》课程知识要点和难点
- The best smart home open source system in 2022: introduction to Alexa, home assistant and homekit ecosystem
- Redis AOF日志
- 深度学习 | 三个概念:Epoch, Batch, Iteration
- 问题随记 —— file /usr/share/mysql/charsets/README from install of MySQL-server-5.1.73-1.glibc23.x86_64 c
- sql 优化
- [must] bm41 output the right view of the binary tree [medium +]
- Create Ca and issue certificate through go language
- Postgresql源码(57)HOT更新为什么性能差距那么大?
猜你喜欢

【必会】BM41 输出二叉树的右视图【中等+】

Overview of edge calculation

Commemorate becoming the first dayus200 tripartite demo contributor

Matplotlib common charts

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

How to display real-time 2D map after rviz is opened

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

Experience of practical learning of Silicon Valley products

问题随记 —— /usr/bin/perl is needed by MySQL-server-5.1.73-1.glibc23.x86_64

有没有一段代码,让你为人类的智慧所折服
随机推荐
[C #] dependency injection and Autofac
vs2015 AdminDeployment.xml
Algolia's search needs are almost closed
Depth first search and breadth first search of graph traversal
Development trend and future direction of neural network Internet of things
股票开户哪个证券公司最好,有安全保障吗
PyCharm调用matplotlib绘图时图像弹出问题怎么解决
常见的积分商城游戏类型有哪些?
[es practice] safe operation mode on ES
JS - use of arguments
y53.第三章 Kubernetes从入门到精通 -- ingress(二六)
TS initial use, TS type
Postgresql源码(57)HOT更新为什么性能差距那么大?
Notes to problems - file /usr/share/mysql/charsets/readme from install of mysql-server-5.1.73-1 glibc23.x86_ 64 c
Linux基础 —— CentOS7 离线安装 MySQL
在长城证券上买基金安全吗?
jpa手写sql,用自定义实体类接收
Write some suggestions to current and future doctoral students to sort out and share
[LeetCode] 最后一个单词的长度【58】
Practical application and extension of plain framework