aJynks
aJynks

Reputation: 689

Is there a container exactly like std::map but not automatically sorted?

Is there an alternative container that has the same functionality as std::map but it dose not automatically sort the contents? As std::map is a "ordered" container.

I have a problem were as I add things to the std::map bar; The string list is automatically getting sorted in alphabetical order.

Everything else in my program works fine.

What I am wondering is if there is a container that has teh exact same functionality as std::map but is not ordered.. so I can do a search and replace std::map with ???:??

Anything like that?

Thanks in advance --Jynks

Upvotes: 2

Views: 240

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726809

There is an std::unordered_map container available starting with C++11. Many compilers that are not yet fully C++11 compatible support it as well. This container does not order your items alphabetically, but being an associative container, it is not required to preserve the insertion (or any other) order of the items.

If you would like to preserve some order of the items in the container and also have it work as an associative container (i.e. a map) you can build your own container as a combination of a linked list and an unsorted map, similar to Java's LinkedHashMap.

Upvotes: 2

Related Questions