STL容器迭代器在何时会失效:(以下内容摘自C++ 98 ISO标准)
1. vector::iterator
[23.2.4.3] vector modifiers
iterator insert(iterator position, const T& x);
void insert(iterator position, size_type n, const T& x);
template
void insert(iterator position, InputIterator first, InputIterator last);
Notes: Causes reallocation if the new size is greater than the old capacity. If no reallocation happens, all the iterators and references before the insertion point remain valid.......
iterator erase(iterator position);
iterator erase(iterator first, iterator last);
Effects: Invalidates all the iterators and references after the point of the erase.
2. list::iterator
[23.2.2.3] list modifiers
iterator insert(iterator position, const T& x);
void insert(iterator position, size_type n, const T& x);
template
void insert(iterator position, InputIterator first, InputIterator last);
void push_front(const T& x);
void push_back(const T& x);
Notes: Does not affect the validity of iterators and references. ......
iterator erase(iterator position);
iterator erase(iterator first, iterator last);
void pop_front();
void pop_back();
void clear();
Effect: Invalidates only the iterators and references to the erased elements.
deque::iterator
[23.2.1.3] deque modifiers
iterator insert(iterator position, const T& x);
void insert(iterator position, size_type n, const T& x);
template
void insert(iterator position, InputIterator first, InputIterator last);
Effects: An insert in the middle of the deque invalidates all the iterators and references to elements of the deque. An insert at either end of the deque invalidates all the iterators to the deque, but has no effect on the validity of references to elements fo the deque.
iterator erase(iterator position);
iterator erase(iterator first, iterator last);
Effects: An erase in the middle of the deque invalidates all the iterators and references to elements of the deque. An erase at either end of the deque invalidates only the iterators and the references to the erased elements.
Assosiative container::iterator
[23.1.2] line 8:
The insert members shall not affect the validity of iterators and references to the container, and the erase members shall invalidate only iterators and references to the erased elements.
阅读(1766) | 评论(0) | 转发(0) |