Please correct me if I am wrong. Thank you!
insert and erase will relocate elements, but elements before the position where insertion/erasure takes place don’t relocate and hence their iterators remain valid.
push_back and pop_back don’t invalidate any iterators.
push_front and pop_front invalidate all iterators.
swap won’t relocate elements, but somehow I think it should invalidate iterators.
push_back()andpush_front()are defined in terms ofinsert(). Similarly,pop_back()andpop_front()are defined in terms oferase().Here’s what the C++03 standard says about iterator invalidation for
insert()(23.2.1.3/1):So
push_front()andpush_back()will invalidate iterators, but references to the elements themselves remain valid.For
erase()at either end (23.2.1.3/4):So
pop_front()andpop_back()only invalidate iterators/references to the element at the end in question.And this is said says this about
swap()for any standard container (23.1/10 "Container requirements"):C++11 adds the following clarifications regarding how the
end()iterator on adequebehaves for these operations. Basically, an iterator toend()should be treated as invalid after aswap()or after erasing the last element in thedeque:I think it would be a good idea to code as if these rules apply even if you’re not yet using a C++11 compiler.