Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C+11 Associative Container that keeps insertion order?

is there, in the c++ "Standard Library", any "Associative" (i.e. "Key-Value") Container/Data Structure, that has the ability, to preserve order, by order of insertion?

I have seen several topics on this, however, it seems, most before C++11.

Some suggest using "boost::multi_index", but, if at all possible, I would "rather" use standard containers/structures.

I see that C++11 has several, apparently, "unordered" associative containers :link.

Are any of these, by some way, "configurable", such that they are only sorted by insertion order?

Thanks!

C

like image 272
ChrisC Avatar asked Sep 21 '13 05:09

ChrisC


People also ask

How do you maintain a set order in C++?

One way is to use two containers, a std::deque to store the elements in insertion order, and another std::set to make sure there are no duplicates. When inserting an element, check if it's in the set first, if yes, throw it out; if it's not there, insert it both in the deque and the set .

Does map maintain insertion order C++?

C++ hash map and hash set which preserves the order of insertion. The ordered-map library provides a hash map and a hash set which preserve the order of insertion in a way similar to Python's OrderedDict. When iterating over the map, the values will be returned in the same order as they were inserted.

How many types of associative containers are there?

The four ordered associative containers are multiset, set, multimap, and map.

What is set associative container?

Introduction. A set is an Associative container which contains a sorted set of unique objects of type Key. Each element may occur only once, so duplicates are not allowed. There are four kind of Associative containers: set, multiset, map and multimap.


1 Answers

No.

You are mixing linear access with random. Not very good bed fellows.

Just use both a vector/list (i.e. order of insertion) along with a map using an index into the former.

like image 121
Ed Heal Avatar answered Sep 21 '22 02:09

Ed Heal