std::list is useful in several corner cases.
However, the general rule for C ++ sequential containers is "if your algorithm is compatible, use std::vector . If your algorithms are incompatible, change your algorithms so you can use std::vector ."
There are exceptions, and here's an attempt to exhaustively list the reasons std::list is the best choice:
When you need to (A) insert in the middle of the container and (B), you need to keep every location of objects in memory stable. Requirement (B) can usually be removed by having an unstable container consisting of pointers to elements, so this is not a serious reason to use std::list .
If you need to (A) insert or remove orders in the middle of the container (B) more often than you need to iterate over the container. This is also an extreme edge case: in order to find an item to remove from a list , you usually need to iterate!
That leads to
you need to (A) insert or remove in the middle of the container and (B) include iterators for all other elements. This becomes a hidden requirement for case 1 and case 2: it is difficult to remove or insert it more often than you repeat when you do not have constant iterators, and the stability of iterators and objects is very interconnected.
The latter case, the splicing case, was once the reason for using std::list .
In C ++ 03, all versions of std::list::splice can (theoretically) run O (1) times. However, the extremely efficient forms of splice required that size be an O (n) operation. C ++ 11 required that the size on a list be O (1), so the maximum performance of splice limited to "splicing a whole other list" and "sub-list self-alignment". In the case of splicing with one element, this is just insertion and deletion. In the case of sub-band splicing, the code should now visit each node in splices only to count them, in order to maintain size as O (1) (except for self-fusion).
So, if you are only executing entire chains of a list or a submenu of your own splice s list , list can perform these operations much faster than other non-list containers.
Yakk
source share