一.vector
所有读操作、swap、std::swap:都不会引起迭代器失效...
clear、operator=、assign:都会引起全部变量迭代器失效
reserve、shrink_to_fit:如果capacity的大小被改变了,则引起全部变量迭代器失效
erase:被删除的变量以及其后面的变量包括end()都迭代器失效
push_back、emplace_back:假如capacity的大小被改变,则引起全部变量迭代器失效。否则只是end()迭代器失效
insert、emplace、resize:假如capacity的大小被改变,则引起全部变量迭代器失效。否则只是在插入位置后面的变量迭代器失效
pop_back:被删除的变量以及end()迭代器失效
二.deque(没有发生删除且只插入在末尾,指针和引用不失效)
All read only operations | Never |
swap, std::swap | The past-the-end iterator may be invalidated (implementation defined) |
shrink_to_fit, clear, insert, emplace, push_front, push_back, emplace_front, emplace_back | Always |
erase | If erasing at begin - only erased elements If erasing at end - only erased elements and the past-the-end iterator |
resize | If the new size is smaller than the old one : only erased elements and the past-the-end iterator If the new size is bigger than the old one : all iterators are invalidated |
pop_front | Only to the element erased |
pop_back | Only to the element erased and the past-the-end iterator |
Invalidation notes
- When inserting at either end of the deque, references are not invalidated by insert and emplace.
- push_front, push_back, emplace_front and emplace_back do not invalidate any references to elements of the deque.
- When erasing at either end of the deque, references to non-erased elements are not invalidated by erase, pop_front and pop_back.
- A call to resize with a smaller size does not invalidate any references to non-erased elements.
- A call to resize with a bigger size does not invalidate any references to elements of the deque.