The complexity of vector::erase() is: Linear in the number of elements to be erased (destruction) plus the number of elements after the last element has been deleted (moving).
It seems to me that it is implemented as a lazy array of growth / contraction. An iterator, a pointer to data, when erased, the following data will be copied to its place. And thus, the iterator that you store will point to some data yet, provided that the data you delete is not the last.
In fact, this may be implementation dependent. However, I think that a lazy increase / compression array is the best option for vector::erase() . [Since this may be implementation dependent, do not rely on something like invalidate ...]
source share