当前位置:网站首页>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;
}
边栏推荐
- Kubernetes resource object introduction and common commands (III)
- Future trend and development of neural network Internet of things
- Chapter 6 data flow modeling
- Behind sharing e-commerce: the spirit of CO creation, symbiosis, sharing, CO prosperity and win-win
- 有没有一段代码,让你为人类的智慧所折服
- SWT / anr problem - SWT causes kernel fuse deadlock
- Yunxin small class | common cognitive misunderstandings in IM and audio and video
- Matplotlib常用图表
- 物联网技术应用属于什么专业分类
- 2022 crane driver (limited to bridge crane) examination questions and simulation examination
猜你喜欢
What professional classification does the application of Internet of things technology belong to
Use vb Net to convert PNG pictures into icon type icon files
ConcurrentSkipListMap——跳表原理
Yunxin small class | common cognitive misunderstandings in IM and audio and video
有没有一段代码,让你为人类的智慧所折服
Door level modeling - after class exercises
What category does the Internet of things application technology major belong to
kubernetes资源对象介绍及常用命令(三)
Huawei HMS core joins hands with hypergraph to inject new momentum into 3D GIS
安全协议重点
随机推荐
Concepts of dictionary, hash table and array
第六章 数据流建模
Why does blocprovider feel similar to provider?
Matplotlib common charts
[must] bm41 output the right view of the binary tree [medium +]
Depth first search and breadth first search of graph traversal
const // It is a const object...class nullptr_t
Yunxin small class | common cognitive misunderstandings in IM and audio and video
[es practice] safe operation mode on ES
SecurityUtils. getSubject(). How to solve the problem that getprincipal() is null
algolia 搜索需求,做的快自闭了...
【必会】BM41 输出二叉树的右视图【中等+】
2022 safety officer-c certificate examination question simulation examination question bank and simulation examination
golang中的iota
华为HMS Core携手超图为三维GIS注入新动能
How to display real-time 2D map after rviz is opened
ADO.NET 之sqlConnection 对象使用摘要
2021 robocom world robot developer competition - preliminary competition of undergraduate group
notBlank 和 notEmpty
The essence of software architecture