Tuesday, January 23, 2007

vector iterator invalidation

There are quite a few member functions of the vector class that can potentially invalidate iterators and element references. The following shows a list of those methods and actions:
  • assign
  • clear
  • erase (invalidates those after the erased element)
  • insert
  • pop_back (may or may not, I am not sure)
  • push_back (if causes reallocation - may not when extra space for more elements reserved)
  • resize
  • reserve
  • swap
  • assignment operator=
  • when the vector object is destroyed

All these are capable of causing reallocations of the vector and hence are capable of invalidating iterators. I will try to make it more simpler : anything that can change size() or capacity()! If there can be exceptions to this statement, please let me know!

Cheers!!!

No comments: