按大小排序地图
我有类似的地图:
map<int, map<int, map<int, int> > > myMap;
order-num | id | order-num-of-relation | relation-id
-----------------------------------------------------
0 | 1 | 0 | 2
-----------------------------------------------------
1 | 2 | 0 | 1
-----------------------------------------------------
| | 1 | 3
-----------------------------------------------------
2 | 3 | 0 | 2
-----------------------------------------------------
1(1)、2(2)、3(1)
,我需要按最后一个地图的大小对该地图进行排序(更改“order-num”)(order-num-of-relation | order-num-of-relation | order-num-of-relation)关系 ID)。
我只需要这样做:
order-num | id | order-num-of-relation | relation-id
-----------------------------------------------------
0 | 1 | 0 | 2
-----------------------------------------------------
1 | 3 | 0 | 2
-----------------------------------------------------
2 | 2 | 0 | 1
-----------------------------------------------------
| | 1 | 3
-----------------------------------------------------
1(1)、3(1)、2(2)
我可以使用“排序”函数并在此处传递自己的排序函数(我可以在其中检查大小并返回真/假),或者做我必须编写明确的排序算法吗?
I have similar map:
map<int, map<int, map<int, int> > > myMap;
order-num | id | order-num-of-relation | relation-id
-----------------------------------------------------
0 | 1 | 0 | 2
-----------------------------------------------------
1 | 2 | 0 | 1
-----------------------------------------------------
| | 1 | 3
-----------------------------------------------------
2 | 3 | 0 | 2
-----------------------------------------------------
1(1), 2(2), 3(1)
and i need to sort (change the "order-num") this map by size of the last map (order-num-of-relation | relation-id).
I just need to do this:
order-num | id | order-num-of-relation | relation-id
-----------------------------------------------------
0 | 1 | 0 | 2
-----------------------------------------------------
1 | 3 | 0 | 2
-----------------------------------------------------
2 | 2 | 0 | 1
-----------------------------------------------------
| | 1 | 3
-----------------------------------------------------
1(1), 3(1), 2(2)
can i use the "sort" function and pass here own sorting function (where i can checking size and returing true/false), or do i have to write explicite sorting algorithm?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不/无法对地图进行排序。它们根据模板参数的可选第三个参数按键自动排序,这是一个函数对象类,用于比较两个元素以确定哪个应该排在前面。 (如果第一个应该在第二个之前,它应该返回 true,否则返回 false)
所以你可以使用这样的东西:
但是由于
map
是你的值,而不是你的键,所以并不完全适合你。You don't/can't sort maps. They are automatically sorted by key based on the optional third parameter to the template arguments, which is a function object class used to compare two elements to determine which should come first. (it should return true if the first should come before the second, false otherwise)
So you can use something like this:
But since
map<int,int>
is your value, and not your key, this won't exactly work for you.您要查找的内容已在 Boost 中使用 MultiIndex 完成。这是一个很好的教程 Boost 介绍如何使用它来解决您对数据收集的要求以及他们对 示例。
当然,使用这个集合对象也可能会改变您存储信息的方式。您将把它放在一个结构中。但是,如果您想将您的信息视为具有按规范唯一顺序的数据库,这是我知道其清洁程度的唯一方法。
另一种选择是在将项目放入 std::map 时创建自己的排序运算符。因此:
老实说,这种方式很痛苦,并且会导致很容易被忽视的逻辑错误。使用 Boost,大多数“棘手”的事情都会为您解决。
What you're looking for has been done in Boost with MultiIndex. Here's a good tutorial from Boost on how you can use it to solve what you're asking of your data collection and their selection of examples.
Of course, using this collection object will probably change how you store the information too. You'll be placing it within a struct. However, if you want to treat your information like a database with a unique order by specification this is the only way I know how that's clean.
The other option is to create your own ordering operator while placing the items in a std::map. Hence:
Honestly this way is a pain and invites a very easily overlooked logic fault. Using Boost, most of the "tricky" stuff is taken care of for you.