开发者

Sorting map by size

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-nu开发者_StackOverflow社区m-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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜