In theory, I would say that the correct data structure is a multi-road tree - preferably something like a B + tree. Traditionally, this is a disk-based data structure, but modern main memory has many similar characteristics due to cache layers and virtual memory.
Iterating the B + tree in order is very efficient, because (1) you only iterate over the linked list of leaf nodes - the branch nodes are not needed, and (2) you get very good locality.
Search, delete and insert arbitrary elements - log (n), as with any balanced tree, but with different constant factors.
When resorting to a tree, it is mainly a question of choosing an algorithm that gives good performance when working with a linked list of blocks (leaf nodes), minimizing the need to use leaf nodes - options for quick sorting or merging seem likely candidates, as soon as the elements are sorted in nodes branching, just pass the summary information through leaf nodes.
BUT - pragmatic, this is only what you would do if you were sure that you needed it. The odds are good that you are better off using a standard container. Algorithm / data structure optimization is the best kind of optimization, but it can still be premature.
Steve314
source share