Unlike the other containers, list
and forward_list
define several algorithms as members. In particular, the list types define their own versions of sort, merge, remove, reverse
, and unique
. The generic version of sort
requires random-access iterators. As a result, sort
cannot be used with list
and forward_list
because these types offer bidirectional and forward iterators, respectively.
The generic versions of the other algorithms that the list types define can be used with lists, but at a cost in performance. These algorithms swap elements in the input sequence. A list can “swap” its elements by changing the links among its elements rather than swapping the values of those elements. As a result, the list-specific versions of these algorithms can achieve much better performance than the corresponding generic versions.
These list
-specific operations are described in Table 10.6. Generic algorithms not listed in the table that take appropriate iterators execute equally efficiently on list
s and forward_lists
s as on other containers.