Good day!
In his "Effective STL" Scott Meyers wrote
A third is to use the information in an ordered container of iterators to iteratively splice the list's elements into the positions you'd like them to be in. As you can see, there are lots of options. (Item 31, second part)
Can someone explain me this way?
More text (to understand the context):
The algorithms sort, stable_sort, partial_sort, and nth_element require random access iterators, so they may be applied only to vectors, strings, deques, and arrays. It makes no sense to sort elements in standard associative containers, because such containers use their comparison functions to remain sorted at all times. The only container where we might like to use sort, stable_sort, partial_sort, or nth_element, but can't, is list, and list compensates somewhat by offering its sort member function. (Interestingly, list::sort performs a stable sort.) If you want to sort a list, then, you can, but if you want to use partial_sort, or nth_element on the objects in a list, you have to do it indirectly. One indirect approach is to copy the elements into a container with random access iterators, then apply the desired algorithm to that. Another is to create a container of list::iterators, use the algorithm on that container, then access the list elements via the iterators. A third is to use the information in an ordered container of iterators to iteratively splice the list's elements into the positions you'd like them to be in. As you can see, there are lots of options.
list
is already generally not recommend because of it is node based (ie, elements are scattered to the wind), and here we introduce another node based container to refer to the list items (via a pointer-like object). Pfiuuu. – Biceset
which is itself node based. – Biceset
,list
andmap
have a requirement that iterators and references must not be invalidated on insert/erase (apart from erasing the element actually pointed to). This can be compounded, a bit, by providing an allocator that packs the nodes together, but it still means a lot of pointers and forth-and-back traversals. – Bice