map按key和value排序

来源:互联网 发布:常州淘宝网络公司 编辑:程序博客网 时间:2024/05/22 17:24
<map>Map 按key排序默认升序map<string, int, less<string> > _map; 降序map<string, int, greater<string> > _map;  自定义第三个参数: struct CmpByKeyLength {     bool operator()(const string& k1, const string& k2) {        return k1.length() < k2.length();     }   };map<string, int, CmpByKeyLength> _map;   Map按value排序(复制到线性表中再排序)typedef pair<string, int> PAIR;vector<PAIR> _vec(_map.begin(), _map.end());   bool cmp_by_value(const PAIR& lhs, const PAIR& rhs) {    return lhs.second < rhs.second;  }  sort(_vec.begin(), _vec.end(), cmp_by_value);或者struct CmpByValue {    bool operator()(const PAIR& lhs, const PAIR& rhs) {      return lhs.second < rhs.second;    }  }; sort(_vec.begin(), _vec.end(), CmpByValue()); 


1 0