#define tle ios_base::sync_with_stdio(false),cin.tie(NULL)
#include<iostream>
#include<cstdio>
#include<bits/stdc++.h>
#include<algorithm>
#include<string.h>
#include<sstream>
#include<vector>
#include<set>
using namespace std;
typedef long long ll;
const int maxn=500005;
typedef pair<int,int> p;
struct cmp
{
//键对用这种方法 来做
//主要是对 key(左边的那个)重载排序
//写为右边的那个会报错
//如果这是 key 的大小的话 map<int,int,great<int> >
// 或者 map<int,int,less<int> >
bool operator()(const p a,const p b)
{
return a.second < b.second;
}
};
bool cmp1(const p a,const p b)
{
//是对 value 进行排序 ,那这里得写 a.second b.second
//虽然 写成 a.first b.first 可以对 key 排序,还是用上面那种方法吧
return a.second<b.second;
}
int main()
{
tle;
int i,j;
map<p,int,cmp>mp1;
mp1.insert({{1,2},123}); //可以 make_pair 也可以直接{ , } 来插入数据
mp1.insert({{2,3},159});
for(auto it=mp1.begin();it!=mp1.end();it++)
cout<<((*it).first).first<<' '<<(*it).second<<endl;
cout<<"-------------------"<<endl;
cout<<endl;
//以下是对 value 排序,需要借助vector+sort 操作
map<int,int>mp2;
mp2.insert({1000,2000});
mp2.insert({100,200});
vector<p>s(mp2.begin(),mp2.end());
sort(s.begin(),s.end(),cmp1);
for(i=0;i<s.size();i++)
cout<<(s[i]).first<<' '<<(s[i]).second<<endl;
return 0;
}
优先队列加入pair 操作
#define tle ios_base::sync_with_stdio(false),cin.tie(NULL)
#include<iostream>
#include<cstdio>
#include<bits/stdc++.h>
#include<algorithm>
#include<string.h>
#include<sstream>
#include<vector>
#include<set>
using namespace std;
typedef long long ll;
const int maxn=500005;
typedef pair<int,int> p;
struct cmp
{
bool operator()(const p a,const p b)
{
return a.first>b.first;
}
};
int main()
{
tle;
int i,j;
priority_queue<p,vector<p>,cmp >mp;
return 0;
}
将pair 放入vector 排序,借助sort+重载函数
#define tle ios_base::sync_with_stdio(false),cin.tie(NULL)
#include<iostream>
#include<cstdio>
#include<bits/stdc++.h>
#include<algorithm>
#include<string.h>
#include<sstream>
#include<vector>
#include<set>
using namespace std;
typedef long long ll;
const int maxn=500005;
typedef pair<int,int> p;
bool cmp(p a,p b)
{
return a.first>b.first;
}
int main()
{
tle;
int i,j;
vector<p>mp;
sort(mp.begin(),mp.end(),cmp);
return 0;
}