1. 程式人生 > >C++ STL map容器的排序(按key或value)

C++ STL map容器的排序(按key或value)

template < class Key,                                     // map::key_type
           class T,                                       // map::mapped_type
           class Compare = less<Key>,                     // map::key_compare
           class Alloc = allocator<pair<const Key,T> >    // map::allocator_type
> class map;

less的實現

template <class T> struct less : binary_function <T,T,bool> {  
  bool operator() (const T& x, const T& y) const  
    {return x<y;}  
}; 

greater實現

template <class T> struct greater : binary_function <T,T,bool> {  
  bool
operator() (const T& x, const T& y) const {return x>y;} };
#include <map>  
#include <string>  
#include <iostream> 
#include <vector> 
using namespace std;  

typedef pair<string, int> PAIR;  

ostream& operator<<(ostream& out, const
PAIR& p) { return out << p.first << "\t" << p.second; } void mapSortKeyLess() { 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)); for (map<string, int>::iterator iter = name_score_map.begin();iter != name_score_map.end(); ++iter) { cout << *iter << endl; } /**output Albert 86 Bing 99 BoB 92 LiMin 90 ZiLinMi 79 **/ } void mapSortKyeGreater() { map<string, int, greater<string> > 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)); for (map<string, int>::iterator iter = name_score_map.begin(); iter != name_score_map.end();++iter) { cout << *iter << endl; } } //---------------- struct CmpByKeyLength { bool operator()(const string& k1, const string& k2) { return k1.length() < k2.length(); } }; // void mapSortKeyCmp() { // map<string, int, CmpByKeyLength> 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)); // for (map<string, int>::iterator iter = name_score_map.begin(); // iter != name_score_map.end(); // ++iter) { // cout << *iter << endl; // } // } // //----------------- // typedef pair<string, int> PAIR; // bool cmp_by_value(const PAIR& lhs, const PAIR& rhs) { // return lhs.second < rhs.second; // } // struct CmpByValue { // bool operator()(const PAIR& lhs, const PAIR& rhs) { // return lhs.second < rhs.second; // } // }; // void mapSortValueCmp() { // 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)); // //把map中元素轉存到vector中 // vector<PAIR> name_score_vec(name_score_map.begin(), name_score_map.end()); // sort(name_score_vec.begin(), name_score_vec.end(), CmpByValue()); // // 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] << endl; // } // } int main() { cout << "mapSortKeyLess" << endl; mapSortKeyLess(); cout << "mapSortKyeGreater" << endl; mapSortKyeGreater(); cout << "mapSortKeyCmp" << endl; mapSortKeyCmp(); // cout << "mapSortValueCmp" << endl; // mapSortValueCmp(); } /** mapSortKeyLess Albert 86 Bing 99 BoB 92 LiMin 90 ZiLinMi 79 mapSortKyeGreater ZiLinMi 79 LiMin 90 BoB 92 Bing 99 Albert 86 **/

For example,
Given [1,1,1,2,2,3] and k = 2, return [1,2].

Note:
You may assume k is always valid, 1 ≤ k ≤ number of unique elements.
Your algorithm’s time complexity must be better than O(n log n), where n is the array’s size.


class Solution {

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

    vector<int> topKFrequent(vector<int>& nums, int k) {
        vector<int> ret;
        if (nums.size() == 0 || k==0)
            return ret;

        map<int, int> numMap;
        for (int i = 0; i < nums.size(); i++) {
            numMap[nums[i]]++;
        }

        vector<PAIR> numMap_vec(numMap.begin(), numMap.end());
        sort(numMap_vec.begin(), numMap_vec.end(), cmp);

        int index = 0;
        for(auto it = numMap_vec.begin(); it != numMap_vec.end(); it++) {
            int number = it->first;
            ret.push_back(number);
            if (++index == k)
                break;
        }

        sort(ret.begin(), ret.end());
        return ret;

    }
};

References