概述

根据迭代器功能的不同,将迭代器分为以下几类:

Iterator Category Ability Providers
Input iterator Reads forward istream
Output iterator Writes forward ostream, inserter
Forward iterator Reads and writes forward  
Bidirectional iterator(双向迭代器) Reads and writes forward and backward list, set, multiset, map, multimap
Random access iterator Reads and writes with random access vector, deque string, array

下面,逐一分析各种迭代器。

输入迭代器

  Input iterators can only step forward element-by-element with read access. Thus, they return values elementwise

  输入迭代器的操作:

Table 7.2. Operations of Input Iterators

Expression Effect
*iter Provides read access to the actual element
iter ->member Provides read access to a member (if any) of the actual element
++iter Steps forward (returns new position)
iter++ Steps forward (returns old position)
Iter1 == iter2 Returns whether two iterators are equal
Iter1 != iter2 Returns whether two iterators are not equal
TYPE(iter) Copies iterator (copy constructor)

  注意:输入迭代器只能够读取一次元素。几乎所有其他迭代器都有输入迭代器的功能。

输出迭代器

  Output iterators are the counterparts of input iterators. They can only step forward with write access. Thus, you can assign new values only element-by-element. You can't use an output iterator to iterate twice over the same range. The goal is to write a value into a "black hole" (whatever that means). So, if you write something for the second time at the same position into the same black hole, it is not guaranteed that you will overwrite a previous value. Table 7.3 lists the valid operations for output iterators. The only valid use of operator * is on the left side of an assignment statement.

Table 7.3. Operations of Output Iterators

Expression Effect
*iter = value Writes value to where the iterator refers
++iter Steps forward (returns new position)
iter++ Steps forward (returns old position)
TYPE (iter) Copies iterator (copy constructor)

前向迭代器

  前向迭代器是输入迭代器和输出迭代器的组合。它包含输入迭代器的所有功能和输出迭代器的大部分功能。

  下面是前向迭代器的操作:

Table 7.4. Operations of Forward Iterators

Expression Effect
*iter Provides access to the actual element
iter-> member Provides access to a member of the actual element
++iter Steps forward (returns new position)
iter++ Steps forward (returns old position)
iter1 == iter2 Returns whether two iterators are equal
iter1 != iter2 Returns whether two iterators are not equal
TYPE() Creates iterator (default constructor)
TYPE(iter) Copies iterator (copy constructor)
iter1 = iter2

Assigns an iterator

  Unlike input iterators and output iterators, forward iterators can refer to the same element in the same collection and process the same element more than once.

为什么前向迭代器只包含输出迭代器的大部分功能,而不是全部功能呢?

  对于输出迭代器,在不检查是不是队列的末尾就写数据是正确的。事实上,是不可以将一个输出迭代器和一个终端迭代器作比较的,因为输出迭代器没有对比的操作。

//OK for output iterators//ERROR for forward iteratorswhile (true) {*pos = foo();++pos;}

  对于前向迭代器,在访问迭代器指向的数据之前,必须确定该迭代器是否是正确的。这就是为什么上面的循环中的代码对前向迭代器是错误的。如果该迭代器指向的是NULL,那么在没有检查之前便去访问未知地址,会出现未定义行为。

//OK for forward iterators//IMPOSSIBLE for output iteratorswhile (pos != coll.end()) {*pos = foo();++pos;}

双向迭代器

  Bidirectional iterators are forward iterators that provide the additional ability to iterate backward over the elements. Thus, they provide the decrement operator to step backward (Table 7.5).

Table 7.5. Additional Operations of Bidirectional Iterators

Expression Effect
-- iter Steps backward (returns new position)
iter-- Steps backward (returns old position)

随机访问迭代器

Random access iterators are bidirectional iterators that can perform random access. Thus, they provide operators for "iterator arithmetic" (in accordance with the "pointer arithmetic" of ordinary pointers). That is, they can add and subtract offsets, process differences, and compare iterators with relational operators such as < and >. Table 7.6 lists the additional operations of random access iterators.

Random access iterators are provided by the following objects and types:

  • Containers with random access (vector, deque)

  • Strings (string, wstring)

  • Ordinary arrays (pointers)

Table 7.6. Additional Operations of Random Access Iterators

Expression Effect
iter[n] Provides access to the element that has index n
iter+=n Steps n elements forward (or backward, if n is negative)
iter-=n Steps n elements backward (or forward, if n is negative)
iter+n Returns the iterator of the nth next element
n+iter Returns the iterator of the nth next element
iter-n Returns the iterator of the nth previous element
iter1-iter2 Returns the distance between iter1 and iter2
iter1<iter2 Returns whether iter1 is before iter2
iter1>iter2 Returns whether iter1 is after iter2
iter1<=iter2 Returns whether iter1 is not after iter2
iter1>=iter2 Returns whether iter1 is not before iter2

The following program demonstrates the special abilities of random access iterators:

// iter/itercat.cpp
#include <vector>#include <iostream>using namespace std;int main(){vector<int> coll;//insert elements from -3 to 9for (int i=-3; i<=9; ++i) {coll.push_back (i);}/* print number of elements by processing the distance between beginning and end* - NOTE: uses operator -for iterators*/cout << "number/distance: " << coll.end()-coll.begin() << endl;/* print all elements* - NOTE: uses operator < instead of operator ! =*/vector<int>::iterator pos;for (pos=coll.begin(); pos<coll.end(); ++pos) {cout << *pos << ' '; }cout << endl;/* print all elements* - NOTE: uses operator [ ] instead of operator **/for (int i=0; i<coll.size(); ++i) {cout << coll.begin() [i] << ' ';}cout << endl;/* print every second element* - NOTE: uses operator +=*/for (pos = coll.begin(); pos < coll.end()-1; pos += 2) {cout << *pos << ' ';}cout << endl;}

The output of the program is as follows:

                 number/distance: 13-3 -2 -1 0 1 2 3 4 5 6 7 8 9-3 -2 -1 0 1 2 3 4 5 6 7 8 9-3 -1 1 3 5 7

转载于:https://www.cnblogs.com/wiessharling/p/3990946.html

STL之Iterator(迭代器)相关推荐

  1. STL 容器和迭代器连载6_顺序容器的操作3

    2019独角兽企业重金招聘Python工程师标准>>> /*- ========================================================== ...

  2. STL 容器和迭代器连载8_访问顺序容器的元素

    2019独角兽企业重金招聘Python工程师标准>>> /*- ========================================================== ...

  3. C++中STL容器利用迭代器删除元素小结

    C++中STL容器利用迭代器删除元素小结 在STL容器中删除元素,容易导致迭代器失效,故应该清楚明白其用法,现在总结一下. 转载自:https://blog.csdn.net/yf_li123/art ...

  4. C++ [STL容器反向迭代器]

    本文已收录至<C++语言和高级数据结构>专栏! 作者:ARMCSKGT STL容器反向迭代器 前言 正文 适配器 反向迭代器 反向迭代器框架 默认成员函数 反向迭代器的遍历 反向迭代器的比 ...

  5. 2021年大数据常用语言Scala(十九):基础语法学习 iterator迭代器

    目录 iterator迭代器 使用迭代器遍历集合 iterator迭代器 scala针对每一类集合都提供了一个迭代器(iterator)用来迭代访问集合 使用迭代器遍历集合 使用iterator方法可 ...

  6. 设计模式(十五):Iterator迭代器模式 -- 行为型模式

    1.概述 类中的面向对象编程封装应用逻辑.类,就是实例化的对象,每个单独的对象都有一个特定的身份和状态.单独的对象是一种组织代码的有用方法,但通常你会处理一组对象或者集合. 集合不一定是均一的.图形用 ...

  7. Java核心API -- 7(Iterator迭代器、Comparable、Comparator比较器)

    1. Iterator迭代器 所有Collection的实现类都实现了iterator方法,该方法返回一个Iterator接口类型的对象,用于实现对集合元素迭代的便利.在java.util包下. 1) ...

  8. 使用Iterator迭代器循环集合

    1.Iterator迭代器用于遍历集合元素,获取迭代器可以使用. 2.Iterator提供了统一遍历集合元素的 方式 ,其提供了用于遍历集合的连个方法----- boolean  hasNext()判 ...

  9. vector容器与iterator迭代器

    vector容器 vector是同一种类型的对象的集合,每个对象都有一个对应的整数索引值.和string对象一样,标准库负责管理存储元素的相关内存.我们把vector称为容器,是因为它可以包含其他对象 ...

  10. Java基础23-集合类2(Set接口,Iterator迭代器)

    一.Set接口简介 根据API,Set接口是一个不包含重复元素的 collection.更确切地讲,set 不包含满足 e1.equals(e2) 的元素对 e1 和 e2,并且最多包含一个 null ...

最新文章

  1. [业界资讯]腾讯QQ同时在线用户数突破8000万
  2. druid抛出的异常------javax.management.InstanceAlreadyExistsException引发的一系列探索
  3. 令牌桶的自定义注解核心API演示
  4. P1352-没有上司的舞会【树形dp】
  5. pom.xml配置文件配置jar(不用记,快速配置)
  6. 原 IOS之NSValue整理
  7. 【有返回值的回溯法】剑指offer——面试题66:矩阵中的路径(回溯法)
  8. Visual Studio蛋疼问题解决(2)
  9. 关于bayes错误率计算公式P[error] = P[error | x]P(x)dx
  10. CAD填充技巧:填充图案
  11. 【Vue五分钟】五分钟了解vue的常用实例方法
  12. Apple Pay 接入
  13. settings.xml
  14. android: 禁止多点触控
  15. dsp正弦信号发生c语言,正弦信号发生器的DSP实现方法及比较
  16. AppleTalk--网络大典
  17. java爱听音乐音乐播放器
  18. 三维实景下的南极科考站是什么样子?
  19. linux 汇编 push rbp,无法从汇编(yasm)代码调用64位Linux上的C标准库函数
  20. 关于ETD.sys的系统蓝屏问题的解决

热门文章

  1. Python笔记——基本数据结构:列表、元组及字典
  2. 你知道Linux和Unix的区别吗
  3. `constexpr`和`const`之间的区别?
  4. OpenVINO 从yml处提取model下载链接,以mobile-ssd为例
  5. socket网络编程——套接字地址结构
  6. mysql用supervisor管理_使用Supervisor管理进程
  7. 智能车大赛信标组_第十五届全国大学生智能汽车竞赛在南京信息工程大学圆满闭幕...
  8. 软件工程转计算机科学与技术,计算机与软件工程学院本科生转专业实施方案
  9. Kafka分布式环境搭建
  10. aspectj annotation- used in spring