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
**/

map按value排序例子
https://leetcode.com/problems/top-k-frequent-elements/
Given a non-empty array of integers, return the k most frequent elements.

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

  1. http://blog.csdn.net/iicy266/article/details/11906189
  2. http://www.cplusplus.com/reference/map/map/
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值