当前位置:网站首页>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;
}
边栏推荐
- Why is PHP called hypertext preprocessor
- 小程序表单校验封装
- Postgresql源码(57)HOT更新为什么性能差距那么大?
- What are the common types of points mall games?
- Door level modeling - after class exercises
- How to display real-time 2D map after rviz is opened
- Future trend and development of neural network Internet of things
- Is it safe to choose mobile phone for stock trading account opening in Shanghai?
- Daily three questions 6.30
- Matplotlib common settings
猜你喜欢
Kubernetes resource object introduction and common commands (III)
De PIP. Interne. CLI. Main Import main modulenotfounderror: No module named 'PIP'
软件架构的本质
2021 robocom world robot developer competition - semi finals of higher vocational group
物联网现状及未来发展趋势
Various global files related to [.Net core] program
from pip._ internal. cli. main import main ModuleNotFoundError: No module named ‘pip‘
Redis RDB snapshot
写给当前及未来博士研究生一些建议整理分享
Door level modeling - after class exercises
随机推荐
SQL optimization
VIM color the catalogue
神经网络物联网的未来趋势与发展
Y53. Chapter III kubernetes from introduction to mastery -- ingress (26)
ADO.NET之SqlDataAdpter对象
Huawei HMS core joins hands with hypergraph to inject new momentum into 3D GIS
Linux foundation - centos7 offline installation of MySQL
【ES实战】ES上的安全性运行方式
from pip._ internal. cli. main import main ModuleNotFoundError: No module named ‘pip‘
The best smart home open source system in 2022: introduction to Alexa, home assistant and homekit ecosystem
2022-07-01:某公司年会上,大家要玩一食发奖金游戏,一共有n个员工, 每个员工都有建设积分和捣乱积分, 他们需要排成一队,在队伍最前面的一定是老板
ADO.NET 之sqlConnection 对象使用摘要
安全协议重点
电商RPA机器人,助力品牌电商抢立流量高点
What is the difference between memory leak and memory overflow?
JPA handwritten SQL, received with user-defined entity classes
Redis RDB快照
力扣今日题-241. 为运算表达式设计优先级
Stm32f030f4 drives tim1637 nixie tube chip
MySQL binlog cleanup