如何自定义比较函数

class Solution {
public:
    vector topKFrequent(vector& words, int k) {
        unordered_map freq;
        for(auto w : words){
            freq[w]++;
        }
        
        auto comp = [&](const pair& a, const pair& b) {
            return a.second > b.second || (a.second == b.second && a.first < b.first);
        };
        typedef priority_queue< pair, vector>, decltype(comp) > my_priority_queue_t;
        my_priority_queue_t  pq(comp);
        
        for(auto w : freq ){
            pq.emplace(w.first, w.second);
            if(pq.size()>k) pq.pop();
        }
        
        vector output;
        while(!pq.empty()){
            output.insert(output.begin(), pq.top().first);
            pq.pop();
        }
        return output;
    }
};

你可能感兴趣的:(如何自定义比较函数)