Using boost multi_index_container to maintain insertion order

At first I started using std::multimapmany values ​​with the same key to store, but then I found that it does not preserve the insertion order between values ​​with the same key. This answer claims that this can be done with help boost::multi_index::multi_index_container, but does not give any example. Looking through the documents, there are no examples of such use, and I cannot make heads or tails of how you should use this thing. I came to expect poor documentation from less used libraries, but that takes the cake. Can someone point me to a tutorial or example that shows that it is used the way I want, or maybe even provide an example?

+5
source share
2 answers

You can achieve this by using boost::multi_indexwith two indexes: ordered_non_unique(which allows values ​​with the same key) and random_access(which will preserve the insertion order).

struct some {
  long key;
  int data;
  int more_data;
  // etc.  
};

typedef multi_index_container<
  some, 
  indexed_by<    
    random_access<>,  // keep insertion order
    ordered_non_unique< member<some, long, &some::key> >
  > 
> some_mic_t;
+6
source

What about

map<int, vector<string> >

or

map<int, list<string> >

@ Cyril: Good answer. I suspect that Boost random_access can be quite slow, as it will force all rows for all keys to be supported in one continuous structure. While the questionnaire simply wants the order to be maintained in each key set of displayed values.

0
source

All Articles