按大小排序地图

发布于 2024-10-14 05:06:25 字数 1378 浏览 17 评论 0原文

我有类似的地图:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

2024-10-21 05:06:25

您不/无法对地图进行排序。它们根据模板参数的可选第三个参数按键自动排序,这是一个函数对象类,用于比较两个元素以确定哪个应该排在前面。 (如果第一个应该在第二个之前,它应该返回 true,否则返回 false)

所以你可以使用这样的东西:

struct myCompare
{
    bool operator() const (const map<int,int> & lhs, const map<int,int> & rhs)
    {
        return lhs.size() < rhs.size();
    }
};

但是由于 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:

struct myCompare
{
    bool operator() const (const map<int,int> & lhs, const map<int,int> & rhs)
    {
        return lhs.size() < rhs.size();
    }
};

But since map<int,int> is your value, and not your key, this won't exactly work for you.

白馒头 2024-10-21 05:06:25

您要查找的内容已在 Boost 中使用 MultiIndex 完成。这是一个很好的教程 Boost 介绍如何使用它来解决您对数据收集的要求以及他们对 示例

当然,使用这个集合对象也可能会改变您存储信息的方式。您将把它放在一个结构中。但是,如果您想将您的信息视为具有按规范唯一顺序的数据库,这是我知道其清洁程度的唯一方法。

另一种选择是在将项目放入 std::map 时创建自己的排序运算符。因此:

struct Orders{
    int order_num;
    int id;
    int order_num_relation;
    int relation_id;

    bool operator<(const Orders& _rhs){
       if(order_num < _rhs.order_num) return true;
       if(order_num == _rhs.order_num){
           if( id < _rhs.id) return true;
           if( id == _rhs.id){
              //and so on, and so on

老实说,这种方式很痛苦,并且会导致很容易被忽视的逻辑错误。使用 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:

struct Orders{
    int order_num;
    int id;
    int order_num_relation;
    int relation_id;

    bool operator<(const Orders& _rhs){
       if(order_num < _rhs.order_num) return true;
       if(order_num == _rhs.order_num){
           if( id < _rhs.id) return true;
           if( id == _rhs.id){
              //and so on, and so on

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文