I’m looking for a C++ container that will enjoy both map container and list container benefits.
map container advantages I would like to maintain:
- O(log(n)) access
- operator[] ease of use
- sparse nature
list container advantages I would like to maintain:
- having an order between the items
- being able to traverse the list easily UPDATE: by a sorting order based on the key or value
A simple example application would be to hold a list of certain valid dates (business dates, holidays, some other set of important dates…), once given a specific date, you could find it immediately “map style” and then find the next valid date “list style”.
std::mapis already a sorted container where you can iterate over the contained items in order. It only provides O(log(n)) access, though.std::tr1::unordered_map(orstd::unordered_mapin C++0x) has O(1) access but is unsorted.Do you really need O(1) access? You have to use large datasets and do many lookups for O(log(n)) not being fast enough.
If O(log(n)) is enough,
std::mapprovides everything you are asking for.