C++map值排序

class Solution {
public:
    static bool cmp(pair<char,int> a, pair<char,int> b){
        return a.second>b.second;
    }
    string frequencySort(string s) {
        string ans="";
        map<char,int> mp;
        for(auto c:s){
            mp[c]++;
        }
        vector<pair<char,int>> vs;
        for(auto kv:mp){
            vs.push_back({kv.first,kv.second});
        }
        sort(vs.begin(),vs.end(),cmp);
        for(auto kv:vs){
            for(int i=0;i<kv.second;i++){
                ans+=kv.first;
            }
        }
        return ans;
    }
};
posted @ 2022-01-30 15:32  wydxry  阅读(370)  评论(0编辑  收藏  举报
Live2D