How can I preserve the order of elements within an unordered set after using insert (or emplace) without allocating during construction?
For details into this problem, here's an example:
You can see how the last insertion destroys the sequence order (I assume by reconstructing a larger set and moving the elements over). I'm looking for a way to preserve the previous order after inserting an element without the need for specifically allocating in the constructor.
An unordered set is, by definition, unordered. There's no defined order, and the iteration order of elements in the set can change at any time.
And allocating something in the constructor is not going to make any difference, either.
If you want a set with a specific iteration order, that's what std::set
is for. However, std::set
always orders by key value, not insertion order.
You may need to combine several containers together, in order to achieve your desired access semantics.