Re: Is pushing into reserved space invalidating iterators?
MathGis wrote:
Is the following code legal?
I'm asking since the iterator debugging facility of VC++2005's stl
gives an assertion failed at runtime.
#include <vector>
int main(int argc, char** argv)
{
std::vector<int> x;
x.reserve(1);
std::vector<int>::iterator i = x.end();
x.push_back(1); // No reallocation
if ( i != x.end()) // Debug Assertion failed: iterators
incompatible?
I don't know exactly what "iterators incompatible" means, but yes, the
iterator is invalidated here.
return *i; // Debug Assertion failed: vector iterator not
dereferencable?
}
I had understood that iterators only become invalid when vector
operations cause reallocation.
That is only part of the truth: Iterators become invalid when an operation
causes reallocation, or when you insert or delete elements that are before
them.
Regards
Jiri Palecek
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]