码迷,mamicode.com
首页 > 编程语言 > 详细

map按照值排序

时间:2020-03-06 13:13:09      阅读:67      评论:0      收藏:0      [点我收藏+]

标签:out   opera   clu   cto   str   second   for   pair   key   

 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按照值排序

标签:out   opera   clu   cto   str   second   for   pair   key   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!