I initially started out using a std::multimap
to store many values with the same key, but then I discovered that it doesn't preserve the insertion order among values with the same key. This answer claims it can be done with boost::multi_index::multi_index_container
, but gives no example. Looking through the docs, there are no examples of that usage, and I can't make heads or tails of how you're supposed to use this thing. I have come to expect poor documentation from the lesser-used boost libraries, but this takes the cake. Can anyone point me to a tutorial or example that shows it used the way I want, or perhaps even provide an example themselves?
Asked
Active
Viewed 3,911 times
6
-
Do you need this to be a multi-map? – Jul 13 '10 at 19:45
-
yes, I do. I have multiple values with the same key. – rmeador Jul 13 '10 at 19:53
2 Answers
7
You could achieve this by using boost::multi_index
with two indices: ordered_non_unique
(which allows values with the same key) and random_access
(which will keep 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;

Kirill V. Lyadvinsky
- 97,037
- 24
- 136
- 212
-
1Supporting this answer, from the boost documentation: **Random access indices are free-order sequences with constant time positional access and random access iterators. Elements in a random access index are by default sorted according to their order of insertion** – MM. Apr 16 '14 at 00:30
-
-1
How about a
map<int, vector<string> >
or
map<int, list<string> >
@Kirill: Good answer. I suspect Boost's random_access can be quite slow, as it will force all strings for all keys to be maintained in a single contiguous structure. Whereas the questioner simply wants order to be preserved within each key's set of mapped values.

Aaron McDaid
- 26,501
- 9
- 66
- 88
-
`random_access` is an additional index. You use `ordered_non_unique` for search, then `random_access` for iterating through the resulting range. It is not slow. – Kirill V. Lyadvinsky Jul 14 '10 at 04:39
-
(I've used multi_index a lot, but not random_index, so I'm not too certain about any of this) @Kirill: Two points: @rmeador wants to be able to "preserve the insertion order among values with the same key". Given a single key, how can the entire random index be used to quickly achieve this? I suspect that the structure I suggested may be the only way to do this. and I meant that the insertion of new elements may be slower than necessary, as the entire random index is potentially out of date. – Aaron McDaid Jul 14 '10 at 17:11
-
1@Kirill: To clarify, once the ordered_non_unique has identified a range of (unsorted) values corresponding to a single key, how do you then (quickly) use the random_access to sort that set of values? That set of values may be evenly spread throughout a large random_access index. Iterating across the random_access range will not keep the keys in order. – Aaron McDaid Jul 14 '10 at 17:14