<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());
map按key和value排序
最新推荐文章于 2021-04-14 18:10:05 发布