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

边栏推荐
猜你喜欢

Stm32f030f4 drives tim1637 nixie tube chip

电商RPA机器人,助力品牌电商抢立流量高点

Experience of practical learning of Silicon Valley products

The best smart home open source system in 2022: introduction to Alexa, home assistant and homekit ecosystem

How to solve the image pop-up problem when pycharm calls Matplotlib to draw

Kubernetes resource object introduction and common commands (III)

Practical application and extension of plain framework

The essence of software architecture

软件架构的本质

Know --matplotlib
随机推荐
Use vb Net to convert PNG pictures into icon type icon files
Matplotlib常用設置
Why is PHP called hypertext preprocessor
第六章 数据流建模
华为HMS Core携手超图为三维GIS注入新动能
ADO.NET之sqlCommand对象
字典、哈希表、数组的概念
Notes to problems - file /usr/share/mysql/charsets/readme from install of mysql-server-5.1.73-1 glibc23.x86_ 64 c
Notblank and notempty
物联网技术应用属于什么专业分类
kubernetes资源对象介绍及常用命令(三)
Daily three questions 6.28
Matplotlib常用图表
安全协议重点
Similarities and differences between the defined identity execution function authid determiner and PostgreSQL in Oracle
【C#】依赖注入及Autofac
Switch to software testing, knowing these four points is enough!
JPA handwritten SQL, received with user-defined entity classes
ARP message header format and request flow
门级建模—课后习题