STL之Iterator

STL之Iterator

Iterator是指针的概括物,但它更一般化,其可以具有的性质:

  1. dereferenceable(可取值的),past the end(可跨越尾端的), singular(可为null的)
  2. 可以比较相等性
  3. 可以比较谁在前
  4. 可以被复制或赋值
  5. 可以提领(dereference)一个型别为Iterator的对象,也即*p有充分的定义
  6. 可累加,前置++和后置++
  7. 可以累减
  8. 可以提领,但不可以对提领的结果赋值,也即p=x不一定有效(const int)
  9. 可以提领并赋值。*p=x有良好定义

STL之Iterator

Input Iterator(只读),Output Iteratror(只写)差异在于8 9,都支持单向遍历
Forward Iterator 支持读写,即对其提领,便会产生左值
Bidirectional Iterator 支持递减,逆向遍历
Random Access Iteartior :++ – p+n ,p-n p[n] p1-p2 p1