Iterators simply point to container elements, so it’s possible for iterators to become invalid when certain container modifications occur. For example, if you invoke
clear on a
vector, all of its elements are removed. If a program had any iterators that pointed to that
vector’s elements before
clear was called, those iterators would now be invalid. Section 23 of the C++ standard discusses all the cases in which iterators (and pointers and references) are invalidated for each Standard Library container. Here we summarize when iterators are invalidated during insert and erase operations.
When inserting into a:
vector is reallocated, all iterators pointing to that
vector are invalidated. Otherwise, iterators ...