I would like some advice on which STL container best meets the following needs:
- The collection is relatively short-lived.
- The collection contains pointers.
- Elements are added only at the end. The order of elements must be maintained.
- The number of elements is unknown and may vary from hundreds to millions. The number is known only after the final element is added.
- I may iterate over the elements several times.
- After all elements are added, I need to sort the collection, based on the objects the pointers refer to.
- After sorting, I may iterate over the elements several more times.
- After that, the collection will be destroyed.
Thread safety is not required.
Here are my thoughts:
list: Requires a separate allocation for each element. More expensive traversal.
vector: Need to be reallocated as the collection grows. Best sort and traversal performance.
deque: Fewer allocations than list and fewer reallocations than vector. I don't know about behavior with respect to sort.
I am currently using list. The flowchart at In which scenario do I use a particular STL container? leads me to deque.
My knowledge of STL is old; I don't know about container types that have been added since 2003, so maybe there's something well-suited that I've never heard of.