With the clarification in the comments, the answer is std::vector
. This is not surprising, as std::list
is rarely the best container for a job.
Adding elements to the end is amortized constant time, and removing an element at a random index is as fast as a list
, as finding elements in a list
typically takes longer than deleting them from a vector
.
Note that if order does not matter, you can swap an arbitrary element of a vector
with the end one, then pop_back
for constant-time random-access erase.
If you regularly iterate over the container, you can remove_if
- erase
in order to erase elements efficiently at the same time. If erasure happens at a different time than iteration, marking elements as 'to be erased' then erasing them on the next iteration can keep things sane.
Another container to consider if the element order does not matter is unordered_set
.