各种容器排序大集合(用到的时候别哭 ( 太好用了 ) )(以后见到补补)

#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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值