C++Map按Value排序
#include <iostream> #include <map> #include <algorithm> #include <string> #include <sstream> #include <vector> using namespace std; typedef pair<string,int> PAIR; struct cmp { bool operator()(const pair<string,int> &p1,const pair<string,int> &p2) { return p1.second > p2.second; } }; int main() { map<string,int>mp; for(int i=0;i<100;i+=10) { stringstream ss; ss<<i; string si = "m" + ss.str(); mp.insert(make_pair(si,i)); } for(map<string,int>::iterator it = mp.begin();it!=mp.end();it++) { cout<< it->first << " " << it->second <<endl; } vector<pair<string,int>>vt(mp.begin(),mp.end()); sort(vt.begin(),vt.end(),cmp()); for(vector<pair<string,int>>::iterator it=vt.begin();it != vt.end();it++) { cout<< it->first << " " <<it->second <<endl; } return 0; }
posted on 2018-09-19 19:27 HelloWorldTotti 阅读(4355) 评论(0) 编辑 收藏 举报