Chinaunix首页 | 论坛 | 博客
  • 博客访问: 146917
  • 博文数量: 43
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 401
  • 用 户 组: 普通用户
  • 注册时间: 2013-07-31 22:55
文章分类

全部博文(43)

文章存档

2015年(1)

2014年(25)

2013年(17)

我的朋友

分类: C/C++

2014-04-22 16:11:48

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.
阅读(1723) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~