当前位置:网站首页>Map and set use pari as the key value. How to define
Map and set use pari as the key value. How to define
2022-06-29 02:48:00 【Madness makes freedom】
For types that do not have a default hash function , Like custom class type ,pair Type, etc , We have to do it ourselves
Specify a hash function . That's why you build directly pair Type of unordered_set Such as unordered_set<pair<int, int>> uset There will be problems
Of course , Self - defined classes :
You can define the structure ( class ) The operator is overloaded when , Of course, when the overloaded operator is a member function , Member functions
Of ( Show ) The number of parameters is one less than the number of operands ,this Bind to left operand ;
When it is not a member function , To define as a friend function , That is, add the keyword before the function friend , At this point, the parameters of the friend function
And the number of general functions is the same
#include <iostream>
#include <algorithm>
#include <set>
#include <unordered_map>
using namespace std;
struct comp
{
int operator()(const std::pair<int, int>& p) const {
return p.first ^ p.second; // The return value is bool, The comparison is &,| Are slower than this one ,
}
};
/**
This sum priority_queue There is a difference ,priority_queue The higher the default value, the higher the priority , By default, sort by less than ,
But you become bigger than , That is to change the less than row to the greater than sort , The lower the value, the higher the priority .
But this is not priority_queue, It's right set or map Sort , Just like sort Of cmp The function has the same function ,
The latter are sorted from small to large by default , You just changed the sort order , Sort from big to small ,
Priority queue , Priority queue , Nature is different from ordinary containers .
And here comp perhaps temp Is just a calling object , Compare two elements in a set , It must take two parameters , How can you and class member functions
Think of it together , Yes , At first, I became a member function of a class, and I put thies I wrote it out , explain C++ Learning is really not good , Of course ,
I only saw C++ primer, There is no system to learn .
*/
struct temp
{
bool operator()(const std::pair<int, int>& p1,const std::pair<int,int> &p2){
if(p1.first!=p2.first)
return p1.first > p2.first; // The return value is bool, The comparison is &,| Are slower than this one ,
else
return p1.second > p2.second;
}
};
/**
For types that do not have a default hash function , Like custom class type ,pair Type, etc , We have to do it ourselves
Specify a hash function . That's why you build directly pair Type of unordered_set
Such as unordered_set<pair<int, int>> uset There will be problems
*/
unordered_map<pair<int,int> ,int,comp> ump; // It has been proved in practice that the coordinates are transformed into long long A little faster than this
int main()
{
set<pair<int,int>, temp>st;
for(int i=1;i<=10;++i)
st.insert({3*i%15,i+4}); //set Only one value can be inserted at a time
for(auto a:st)
cout << a.first << ' ' << a.second << endl;
return 0;
}
边栏推荐
- 使用gdb添加断点的几种方式
- PHP的exec函数
- 【一起上水硕系列】Day 6-强肝学术论文!最细解释!
- How to add the live video function to the website built by your own live video software (build a live video website)
- Pvcreate ASM disk causes abnormal recovery of ASM disk group - sparing separation
- 如何在关闭socket连接的时候跳过TIME_WAIT的等待状态
- 2022年启牛学堂证券开户安全的嘛?
- PMP项目管理概述
- Use photoshop2022 to create a wonderful gradient effect for pictures
- 18. `bs對象.節點名.next_sibling` 獲取兄弟節點
猜你喜欢
随机推荐
After today, I look forward to the new year's eve of the year of the rabbit
allegro对走好的线取消走线的方法
Talk about SQL optimization
mark
Deploy redis high availability cluster
[untitled]
PHP的exec函数
Table implements alternative adaptation through pseudo classes
[linear algebra] 1.2 total permutation and commutation
MySQL binlog log cleanup
allegro 设计中显示网络飞线或关闭网络飞线的方法
Redis master-slave replication
Ctfhub web SQL injection - integer injection
Sysbench Pressure Test Oracle (installation and use examples)
音响是如何把微弱声音放大呢
PWN beginner level0
String output
thinkphp5.1 runtime文件改成777权限了, 还是无法写入
PMP商业分析概述
99乘法表






![[linear algebra] 1.1 second and third order determinants](/img/ea/70b59c64d3287a887e371a9181fe45.png)

