【leetcode】c++ map按照value排序

来源:互联网 发布:淘宝企业开店 编辑:程序博客网 时间:2024/05/17 10:05

目的:map[key, value],按照value排序


一、加头文件 #include<algorithm>


二、加入代码

typedef pair<string, int> PAIR;int cmp(const PAIR& x, const PAIR& y){return x.second > y.second;}

vector<PAIR> pair_vec;for(map<string,int>::iterator map_iter = name.begin();map_iter!=name.end();++map_iter){pair_vec.push_back(make_pair(map_iter->first, map_iter->second));}sort(pair_vec.begin(), pair_vec.end(),cmp);

for(vector<PAIR>::iterator curr = pair_vec.begin(); curr!=pair_vec.end(); ++curr){cout<<curr->second<<''<<curr->second<<endl;}


0 0
原创粉丝点击