std :: map,如何按值排序,然后按键 [英] std::map, how to sort by value, then by key

查看:158
本文介绍了std :: map,如何按值排序,然后按键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要按值排序地图,然后按键。我有一个这样的内容的地图...

  1现实的
8真的
4原因
3合理
1合理
1重组
1重新组装
2识别
92记录$ ​​b $ b 48记录
7 recs

我需要按顺序获取值,但是踢球者是按照字母顺序排列的值是有序的。什么是最好的方法?

解决方案

std :: map 将通过对其元素进行排序。




您可以使用 code> std :: vector< std :: pair< K,V> 然后使用 std :: sort 通过 std :: stable_sort

  std :: vector< std: :一对< K,V>>项目; 

//填充项

//按值排序使用std :: sort
std :: sort(items.begin(),items.end() ,value_comparer);

//按键排序使用std :: stable_sort
std :: stable_sort(items.begin(),items.end(),key_comparer);

第一个排序应该使用 std :: sort nlog(n),然后使用 c $ c $ std :: stable_sort 在最坏的情况下,n(log(n))^ 2



请注意,虽然 std :: sort 被选为性能原因,为了正确排序,需要 std :: stable_sort ,因为您希望保留按顺序排列的顺序。






@gsf在评论中注明,您只能使用 em> std :: sort 如果您选择比较的比较器,如果它们相等,排序

  auto cmp = [](std :: pair< ; K,V> const& a,std :: pair< K,V> const& b)
{
return a.second!= b.second? a.second b.second:a.first< b.first;
};
std :: sort(items.begin(),items.end(),cmp);

应该是高效的。但是请等待一个更好的方法:存储 std :: pair< V,K> 而不是 std :: pair< K,V> 然后你根本不需要任何比较器 std :: pair 的标准比较器就足够了,因为它比较了 first (这是 V )首先是第二个这是 K

  std :: vector< std :: pair< V,K>>项目; 
// ...
std :: sort(items.begin(),items.end());

这应该很棒。


I need to sort a map by value, then by key. I have a map with contents like this...

  1  realistically
  8         really
  4         reason
  3     reasonable
  1     reasonably
  1     reassemble
  1    reassembled
  2      recognize
 92         record
 48        records
  7           recs

I need to get the values in order, but the kicker is that the keys need to be in alphabetical order after the values are in order. What is the best way to go about this?

解决方案

std::map will sort its elements by keys. It doesn't care about the values when sorting.

You can use std::vector<std::pair<K,V>> then sort it using std::sort followed by std::stable_sort:

std::vector<std::pair<K,V>> items;

//fill items

//sort by value using std::sort
std::sort(items.begin(), items.end(), value_comparer);

//sort by key using std::stable_sort
std::stable_sort(items.begin(), items.end(), key_comparer);

The first sort should use std::sort since it is nlog(n), and then use std::stable_sort which is n(log(n))^2 in the worst case.

Note that while std::sort is chosen for performance reason, std::stable_sort is needed for correct ordering, as you want the order-by-value to be preserved.


@gsf noted in the comment, you could use only std::sort if you choose a comparer which compares values first, and IF they're equal, sort the keys.

auto cmp = [](std::pair<K,V> const & a, std::pair<K,V> const & b) 
{ 
     return a.second != b.second?  a.second < b.second : a.first < b.first;
};
std::sort(items.begin(), items.end(), cmp);

That should be efficient.

But wait, there is a better approach: store std::pair<V,K> instead of std::pair<K,V> and then you don't need any comparer at all — the standard comparer for std::pair would be enough, as it compares first (which is V) first then second which is K:

std::vector<std::pair<V,K>> items;
//...
std::sort(items.begin(), items.end());

That should work great.

这篇关于std :: map,如何按值排序,然后按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆