Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve the elements from map in the order of insertion?

Tags:

c++

map

boost

I have a map which stores <int, char *>. Now I want to retrieve the elements in the order they have been inserted. std::map returns the elements ordered by the key instead. Is it even possible?

like image 276
rahul.deshmukhpatil Avatar asked Dec 19 '22 22:12

rahul.deshmukhpatil


2 Answers

If you are not concerned about the order based on the int (IOW you only need the order of insertion and you are not concerned with the normal key access), simply change it to a vector<pair<int, char*>>, which is by definition ordered by the insertion order (assuming you only insert at the end).

If you want to have two indices simultaneously, you need, well, Boost.MultiIndex or something similar. You'd probably need to keep a separate variable that would only count upwards (would be a steady counter), though, because you could use .size()+1 as new "insertion time key" only if you never erased anything from your map.

like image 165
Bartek Banachewicz Avatar answered Dec 22 '22 10:12

Bartek Banachewicz


Now I want to retrieve the elements in the order they have been inserted. [...] Is it even possible?

No, not with std::map. std::map inserts the element pair into an already ordered tree structure (and after the insertion operation, the std::map has no way of knowing when each entry was added).

You can solve this in multiple ways:

  1. use a std::vector<std::pair<int,char*>>. This will work, but not provide the automatic sorting that a map does.

  2. use Boost stuff (@BartekBanachewicz suggested Boost.MultiIndex)

  3. Use two containers and keep them synchronized: one with a sequential insert (e.g. std::vector) and one with indexing by key (e.g. std::map).

  4. use/write a custom container yourself, so that supports both type of indexing (by key and insert order). Unless you have very specific requirements and use this a lot, you probably shouldn't need to do this.

like image 37
utnapistim Avatar answered Dec 22 '22 10:12

utnapistim