std::list
is useful in a few corner cases.
However, the general rule of C++ sequential containers is "if your algorithm are compatible, use std::vector
. If your algorithms are not compatible, modify your algorithms so you can use std::vector
."
Exceptions exist, and here is an attempt to exhaustively list reasons that std::list
is a better choice:
When you need to (A) insert into the middle of the container and (B) you need each objects location in memory to be stable. Requirement (B) can usually be removed by having a non-stable container consisting of pointers to elements, so this isn't a strong reason to use std::list
.
When you need to (A) insert or delete in the middle of the container (B) orders of magnitude more often than you need to iterate over the container. This is also an extreme corner case: in order to find the element to delete from a list
, you usually need to iterate!
Which leads to
you need (A) insert or delete in the middle of the container and (B) have iterators to all other elements remain valid. This ends up being a hidden requirement of case 1 and case 2: it is hard to delete or insert more often than you iterate when you don't have persistant iterators, and the stability of iterators and of objects is highly related.
the final case, is the case of splicing was once a reason to use std::list
.
Back in C++03, all versions of std::list::splice
could (in theory) be done in O(1) time. However, the extremely efficient forms of splice
required that size
be an O(n) operation. C++11 has required that size
on a list
be O(1), so splice
's extreme efficiency is limited to the "splicing an entire other list" and "self-splicing a sublist" case. In the case of single element splice, this is just an insert and delete. In the case of sub range splice, the code now has to visit every node in the splice just to count them in order to maintain size
as O(1) (except in self-splicing).
So, if you are doing only whole-list
splices, or self-list subrange splice
s, list
can do these operations much faster than other non-list containers.