Replace the position of two iterators in a container

I want to swap the two iterators first and second in the container, and also save them after the operation. Moreover, if I have another iterator pointing to the same value as first , I want to keep it valid (keep it on the same value). Is it possible to achieve with any container STL?

+4
source share
2 answers

std::list allows you to combine a fragment of one list into another (removing it from the first list) without canceling any iterators, links or pointers, and the list of sources and the list of recipients can be the same object.

(This is in line with the C ++ 11 project standard, I don't know if the same C ++ 03 standard says.)

0
source

Have you tried to replace them?

 #include <algorithm> // ... swap(first, second); 

(In C ++ 0x, #include significantly smaller <utility> header is enough.)

Moreover, if I have another iterator pointing to the same value as first , I want it to be correct too (keep it on the same value).

Just override other after replacement:

 swap(first, second); other = first; 
+2
source

All Articles