根据价值对地图进行排序 [英] sort a Map based on value

查看:94
本文介绍了根据价值对地图进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

谁能告诉我如何根据值对地图进行排序.
例如

Hi all,

Can anybody tell me how to sort a map based on the values.
e.g.

Map[key1] = box3;
Map[key2] = box1;
Map[key3] = box4;
Map[key4] = box2;


我想要这样的结果,


i want result like,

Map[key2] = box1;
Map[key4] = box2;
Map[key1] = box3;
Map[key3] = box4;


预先感谢,
RKR


thanks in advance,
RKR

推荐答案

为容器提供键的类型,并按映射中值的排序顺序填充容器.因此,您可以通过已排序的键容器访问已排序的值.但是,每当修改地图容器时,都需要更新和重新使用密钥容器.

另一种选择是填充一个multipmap容器,其中map< key_type,val_type>变成键,键变成值:multimap< val_type,key_type>.
Provide of a container with the type of the key, and populate the container in the sort order of the values in the map. Thus, you can access the sorted values via the sorted key container. However, you will need to update and resort the key container whenever you modify the map container.

An alternative would be to populate a multipmap container where the values of the map<key_type, val_type> become keys, and the keys become values: multimap<val_type, key_type>.


我怀疑stl :: map可以在上面帮助您,您要求它打破其基本原理.我相信您已经为此编写了自己的自定义数据结构.

此外,实际上您将实现基于价值的排序吗?如果您要逐个迭代而不是根据密钥查找数据,则最好寻找其他容器而不是映射
I doubt stl::map can help you in above, you asking it to break it basic principle. I believe you have write your own custom data structure for this.

Also, what actually you going to achieve to sort it on the basis of value? if you are going to iterate one by one instead of finding data based on key, better look for other container instead of map


这篇关于根据价值对地图进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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