how to keep the input order while inserting in multimap

I wanted to maintain the input order while inserting data to a multimap.
my key is a int , and
value is struct.

but when i print the data using an iterator they come out in a sorted fashion, as it maintains the data in a balanced binary tree and traversed inorder.

input key value
1 a
2 b
1 c
3 d

Output is

a
c
b
d


but i wanted to have
a
b
c
d

You could use a more complex key that stores both the actual key, and an ordinal value. Then write the comparison function so that it compares only the ordinal.
Note that this approach doesn't require a multimap, even if a key is inserted more than once.
Why are you using a map/multimap if you don't want the elements ordered by the key?

Why not just use a vector or deque or list or some other unordered container?

If you want to sort by both key and value, then use boost::multi_index_container.
Probably because he needs to at one moment get the elements based on the key.

And I just realized that you can't easily get the elements with the method I mentioned. Yeah, I agree. A vector is a better solution.
Topic archived. No new replies allowed.