将map按照value排序

#include<bits/stdc++.h>
//将map按照value排序
using namespace std;

typedef pair<string, int> PAIR;
bool cmp_by_value(const PAIR& lhs, const PAIR& rhs){
    return lhs.second < rhs.second;
}
int main()
{
    map<string, int> name_score_map;
    name_score_map["Limin"] = 90;
    name_score_map["ZiLinMi"] = 79;
    name_score_map["BoB"] = 92;
    name_score_map.insert(make_pair("Bing", 99));
    name_score_map.insert(make_pair("Albert", 86));
    vector<PAIR> name_score_vec(name_score_map.begin(), name_score_map.end());
    sort(name_score_vec.begin(), name_score_vec.end(), cmp_by_value);
    for(int i = 0;i != name_score_vec.size();++i){
        cout<<name_score_vec[i].first<<" "<<name_score_vec[i].second<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_24624539/article/details/108805373