If I swap two vectors, will their iterators remain valid, now just pointing to the “other” container, or will the iterator be invalidated?
That is, given:
using namespace std;
vector<int> x(42, 42);
vector<int> y;
vector<int>::iterator a = x.begin();
vector<int>::iterator b = x.end();
x.swap(y);
// a and b still valid? Pointing to x or y?
It seems the std mentions nothing about this:
[n3092 – 23.3.6.2]
void swap(vector<T,Allocator>& x);Effects:
Exchanges the contents and capacity()
of *this with that of x.
Note that since I’m on VS 2005 I’m also interested in the effects of iterator debug checks etc. (_SECURE_SCL)
The behavior of swap has been clarified considerably in C++11, in large part to permit the Standard Library algorithms to use argument dependent lookup (ADL) to find swap functions for user-defined types. C++11 adds a swappable concept (C++11 §17.6.3.2[swappable.requirements]) to make this legal (and required).
The text in the C++11 language standard that addresses your question is the following text from the container requirements (§23.2.1[container.requirements.general]/8), which defines the behavior of the
swapmember function of a container:In your example,
ais guaranteed to be valid after the swap, butbis not because it is an end iterator. The reason end iterators are not guaranteed to be valid is explained in a note at §23.2.1/10:This is the same behavior that is defined in C++03, just substantially clarified. The original language from C++03 is at C++03 §23.1/10:
It’s not immediately obvious in the original text, but the phrase “to the elements of the containers” is extremely important, because
end()iterators do not point to elements.