首页 > 编程语言 > 详细

map按照值排序

时间:2020-03-06 12:42:45      阅读:41      评论:0      收藏:0      [点我收藏+]
 1 #include<bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 map<int,int/*,greater<int>*/>mp;
 6 //Tip: map按照key从大到小排序 map<int,int,greater<int>> mp;
 7 
 8 bool cmp(const pair<int,int> &a, const pair<int,int> &b){
 9     return a.second > b.second;
10 }
11 
12 struct cmp2{
13     bool operator () (const pair<int,int> &a, const pair<int,int> &b) const{
14         return a.second > b.second;
15     }
16 };
17 
18 int main(){
19     
20     for(int i=1; i<=10; i++){
21         mp[i] = 10 -i;
22     }
23     
24     vector<pair<int,int>> v(mp.begin(),mp.end());
25     sort(v.begin(),v.end(),cmp);
26     //sort(v.begin(),v.end(),cmp2());
27     for(int i=0; i<v.size(); i++){
28         cout << v[i].first << " " << v[i].second << endl;
29     }
30     return 0;
31 } 

 

map按照值排序

原文:https://www.cnblogs.com/zhangqiling/p/12425787.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!