c++ - Does pop_back() really invalidate *all* iterators on an std::vector? -
std::vector<int> ints; // ... fill ints random values for(std::vector<int>::iterator = ints.begin(); != ints.end(); ) { if(*it < 10) { *it = ints.back(); ints.pop_back(); continue; } it++; }
this code not working because when pop_back()
called, it
invalidated. don't find doc talking invalidation of iterators in std::vector::pop_back()
.
do have links that?
the call pop_back()
removes last element in vector , iterator element invalidated. pop_back()
call not invalidate iterators items before last element, reallocation that. josuttis' "c++ standard library reference":
inserting or removing elements invalidates references, pointers, , iterators refer following element. if insertion causes reallocation, invalidates references, iterators, , pointers.
Comments
Post a Comment