STL之Iterator

来源:互联网 发布:扁平插画 知乎 编辑:程序博客网 时间:2024/06/10 09:02

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有良好定义

这里写图片描述

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

原创粉丝点击