I wonder what is the rationale behind making std::list<>::splice
to invalidate the iterators that refer to the subsequence being spliced into the new container. This looks kinda illogical to me, especially in light of standard std::container::swap
specification. According to the language standard std::container::swap
does not invalidate any iterators. This is a perfectly reasonable practical specification. However, I'd say that std::list<>::splice
would also benefit greatly from iterator-preserving behavior.
I understand that there might be some purely academic considerations based on the concepts of iterator reachability, etc. But at the same time splice
is a std::list
-specific operation, meaning that providing a custom-tailored specification for it probably would not make a serious conceptual damage to the STL design in general.
So what was it? Would it outlaw or overcomplicate some practical implementations of std::list
, which I fail to recognize?
All iterators and the references are invalidated if the inserted item is not inserted at the end of the deque. If the items are deleted from any of the position except the end position, then all iterators will be invalidated. Same as like insert or erase.
You could avoid moving elements of the container by maintaining a free-list (see http://www.memorymanagement.org/glossary/f.html#free.list). To avoid invalidation of references to elements you can use a std::deque if you do not insert or erase in the middle. To avoid invalidation of iterators you can use a std::list.
No, they should not get invalidated after a move operation.
Consider using std::list if: You need to store many items but the number is unknown. You need to insert or remove new elements from any position in the sequence. You do not need efficient access to random elements.
In C++11 splice
does not invalidate the iterators, but make them refer to the appropriate elements in the *this
container. This is all described in 23.3.5.5.
If the containers have customised, unequal (non compatible) allocators, you cannot swap pointers, you have no choice but to really copy elements around.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With