当前位置:网站首页>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;
}
边栏推荐
- EMC、EMI、EMS的關系
- In the name of love, fresh e-commerce companies rush to sell flowers on Valentine's Day
- PHP的exec函数
- 信息学奥赛一本通 1361:产生数(Produce) | 洛谷 P1037 [NOIP2002 普及组] 产生数
- [linear algebra] 1.1 second and third order determinants
- Relations EMC, EMI, EMS
- 使用gdb添加断点的几种方式
- Talk about SQL optimization
- 微信小程序自定义组件
- Some tests on complementary wasm environment
猜你喜欢

What is a thread pool?

The thinkphp5.1 runtime file has been changed to 777 permission, but cannot be written
![[Algèbre linéaire] 1.1 déterminant du deuxième et du troisième ordre](/img/ea/70b59c64d3287a887e371a9181fe45.png)
[Algèbre linéaire] 1.1 déterminant du deuxième et du troisième ordre

Double click events and click events

How does sound amplify weak sounds

PWN攻防世界guess_num

微信小程序安全登录,必须先校验微信返回openid,确保信息唯一性。

sql连续登录问题

认证培训|StreamNative Certification 培训第2期

leetcode 统计无向图中无法互相到达点对数
随机推荐
PWN攻防世界guess_num
After today, I look forward to the new year's eve of the year of the rabbit
Have you learned the common SQL interview questions on the short video platform?
安装mysql5.7 并修改密码
逆序对对数计算,顺序对对数计算——归并排序
SQL continuous login problem
Informatics Olympiad 1361: Produce
PHP XML expat parser
mark
【一起上水硕系列】Day 6-强肝学术论文!最细解释!
sql训练01
1110: nearest common ancestor (function topic)
Calculate rectangular area
Google Maps API v3~ simply turn off infoindow- Google Map API v3 ~ Simply Close an infowindow?
Quelques tests pour compléter l'environnement wasm
Deploy redis high availability cluster
Ctfhub web password weak password
音响是如何把微弱声音放大呢
PWN attack and defense world guess_ num
Ctfhub web SQL injection - integer injection