What are the usual rules for Iterator invalidation when operating over STL container classes(Vector,Dequeue,list,map,multimap,set,multiset). Is it possible to categorize and sum up some general rules/guidelines that a C++ STL programmer must be aware of while working with containers and their Iterators?
What are the usual rules for Iterator invalidation when operating over STL container classes(Vector,Dequeue,list,map,multimap,set,multiset).
Share
These rules are container specific. In fact, these are important criteria for deciding which container you use.
For instance, iterators to an
std::vectorcan get invalidated when an object is inserted (depends in where the object is inserted and if reallocation takes place), and they get invalidated when an object is removed before the iterator. Anstd::listdoes not have this problem. Inserting and removing objects (except for the object the iterator points to) does not invalidate the iterator.SGI provides good documentation on this.