简体   繁体   English

如何按一个值属性对海图进行排序?

[英]How to sort massive map by one value property?

I have a map<std::string, myStruct> I wonder how to sort items in map by int property that is in myStruct.order and if 2 or more of myStruct orders are same throw list of keys (strings) that make map unsortable by that field of myStruct? 我有一个map<std::string, myStruct>我想知道如何通过myStruct.order的int属性对map中的项目进行排序,以及是否有2个或更多myStruct命令是相同的抛出键(字符串)的列表,从而使地图无法排序靠myStruct的那个领域? Is there any fancy way of doing in in C++03 (may be with boost)? 在C ++ 03中是否有任何奇特的方法(可能是通过Boost实现的)?

boost has a complete (and complex) functionality in MultiIndex , but if I understand your requirements, it's overkill in this case. boost在MultiIndex中具有完整(且复杂)的功能,但是如果我理解您的要求,那么在这种情况下就太过分了。 A fairly easy way could be to build a list of pointers to myStruct and sort. 一个相当简单的方法是建立指向myStruct和sort的指针列表。 Then you can easily check for duplicate keys (these became adjacents). 然后,您可以轻松检查重复的键(这些键成为相邻键)。

sort should use a functor of type less<const myStruct*> , ie sort应该使用类型less<const myStruct*>的函子,即

bool compare_orders(const myStruct* a, const myStruct* b) { return a->order < b->order; }

You can't do this, using std::map - the first in the pair is used for comparing (it's called key ). 您不能使用std::map做到这一点- pairfirst用于比较(称为key )。 In your case, the std::string is the key . 在您的情况下, std::string

You can use std::set< std::pair< std::string, MyStruct > > and then implement operator< for two std::pair< std::string, MyStruct > -s. 您可以使用std::set< std::pair< std::string, MyStruct > > ,然后对两个std::pair< std::string, MyStruct > -s实施operator<

Or, you can change the std::map 's definition, if it's possible/allowed/suitable/etc. 或者,如果可能/允许/合适/等,则可以更改std::map的定义。 It really depends on what you're trying to do and what you're allowed to do. 这实际上取决于您要尝试执行的操作以及您被允许执行的操作。

Or some other container (that keeps the order of elements, as inserted - like std::list , std::vector , etc.), and then using std::sort or container's sort method, if exists. 或其他一些容器(保持插入时元素的顺序,例如std::liststd::vector等),然后使用std::sort或容器的sort方法(如果存在)。

What meaning is there in "sorting" a map? 将地图“分类”有什么意思? Sure, the map may be internally organized as a BST or something to make access faster, but conceptionally the elements of the map are in no particular order. 当然,可以在内部将地图组织成BST或某种形式,以加快访问速度,但是从概念上讲,地图的元素没有特定的顺序。

Other than that, you CAN supply an ordering method for the keys (because even if you do organize the map in a particular way, it is by the order of the keys, not by that of the values), but not for the values with the third template argument for map. 除此之外,您还可以为键提供一种排序方法(因为即使您以某种特定的方式组织地图,也可以通过键的顺序而不是值的顺序来进行排序),但不能使用map的第三个模板参数。

I agree with the other answers that it's not clear why you need to sort the map. 我同意其他答案,但不清楚为什么需要对地图进行排序。 But you can use a bidirectional map to let you view both myStruct and string as keys. 但是您可以使用双向映射来让您同时将myStruct和string作为键进行查看。

There's one in boost: 有一个助推器:

http://www.boost.org/doc/libs/1_48_0/libs/bimap/doc/html/index.html http://www.boost.org/doc/libs/1_48_0/libs/bimap/doc/html/index.html

There's an example here 这里有一个例子

http://www.boost.org/doc/libs/1_48_0/libs/bimap/doc/html/boost_bimap/examples/simple_bimap.html http://www.boost.org/doc/libs/1_48_0/libs/bimap/doc/html/boost_bimap/examples/simple_bimap.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM