STL之Iterator(迭代器)
概述
根据迭代器功能的不同,将迭代器分为以下几类:
| 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 iterators
while (true) {
*pos = foo();
++pos;
}
对于前向迭代器,在访问迭代器指向的数据之前,必须确定该迭代器是否是正确的。这就是为什么上面的循环中的代码对前向迭代器是错误的。如果该迭代器指向的是NULL,那么在没有检查之前便去访问未知地址,会出现未定义行为。
//OK for forward iterators
//IMPOSSIBLE for output iterators
while (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 9
for (int i=-; i<=; ++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=; i<coll.size(); ++i) {
cout << coll.begin() [i] << ' ';
}
cout << endl; /* print every second element
* - NOTE: uses operator +=
*/
for (pos = coll.begin(); pos < coll.end()-; pos += ) {
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
STL之Iterator(迭代器)的更多相关文章
- STL之iterator(迭代器)
3.迭代器简单介绍 除了使用下标来訪问vector对象的元素外,标准库还提供了訪问元素的方法:使用迭代器.迭代器是一种检查容器内元素而且遍历元素的数据类型. 百科释义: 迭代器(iterator)是一 ...
- C++ Iterator迭代器介绍及Iterator迭代器用法代码举例
C++ Iterator迭代器介绍 迭代器可被用来访问一个容器类的所包函的全部元素,其行为像一个指针.举一个例子,你可用一个迭代器来实现对vector容器中所含元素的遍历.有这么几种迭代器如下: 迭代 ...
- ES6笔记(6)-- Set、Map结构和Iterator迭代器
系列文章 -- ES6笔记系列 搞ES6的人也是够无聊,把JS弄得越来越像Java.C++,连Iterator迭代器.Set集合.Map结构都出来了,不知道说什么好... 一.简单使用 1. iter ...
- vector容器+iterator迭代器
关于vector容器的详细描述,可参考:http://www.jb51.net/article/41648.htm 关于iterator迭代器的描述,可参考http://www.cppblog.c ...
- 【转】Java学习之Iterator(迭代器)的一般用法 (转)
[转]Java学习之Iterator(迭代器)的一般用法 (转) 迭代器(Iterator) 迭代器是一种设计模式,它是一个对象,它可以遍历并选择序列中的对象,而开发人员不需要了解该序列的底层结构.迭 ...
- 设计模式(十五):Iterator迭代器模式 -- 行为型模式
1.概述 类中的面向对象编程封装应用逻辑.类,就是实例化的对象,每个单独的对象都有一个特定的身份和状态.单独的对象是一种组织代码的有用方法,但通常你会处理一组对象或者集合. 集合不一定是均一的.图形用 ...
- C#:iterator 迭代器/partial class 分布类/泛型
C#:iterator 迭代器/partial class 分布类/泛型 iterator 迭代器 写个最简单的迭代,(迭代一个字符串数组): 1.实现接口中的方法: 1 using System; ...
- [设计模式] Iterator - 迭代器模式:由一份奥利奥早餐联想到的设计模式
Iterator - 迭代器模式 目录 前言 回顾 UML 类图 代码分析 抽象的 UML 类图 思考 前言 这是一包奥利奥(数组),里面藏了很多块奥利奥饼干(数组中的元素),我将它们放在一个碟子上慢 ...
- Python 中 Iterator(迭代器)和Iterable(迭代对象)的区别
直接可以用作for循环的数据类型有以下几种: tuple.list.dict.str等, 上述数据类型可以用作for循环的叫做可迭代对象Iterable.可以使用isinstance判断一个对象是否是 ...
- 使用Iterator迭代器循环集合
1.Iterator迭代器用于遍历集合元素,获取迭代器可以使用. 2.Iterator提供了统一遍历集合元素的 方式 ,其提供了用于遍历集合的连个方法----- boolean hasNext()判 ...
随机推荐
- cas sso单点登录系列2:cas客户端和cas服务端交互原理动画图解,cas协议终极分析
转:http://blog.csdn.net/ae6623/article/details/8848107 1)PPT流程图:ppt下载:http://pan.baidu.com/s/1o7KIlom ...
- Linux shell日常命令和技巧
转自:http://www.vaikan.com/linux-shell-tips-and-tricks/ 原文:http://www.techbar.me/linux-shell-tips/ 使用L ...
- POJ2566-Bound Found (尺取法)
POJ2566-Bound Found 题目大意:给出一段长度为n的数列,数列中的元素有正有负,求一段连续的区间,使得该区间的和的绝对值最接近给定的值 尺取法一般适用于对一段连续的区间的和进行处理的情 ...
- css 日常
去掉input边框 outline:none; 不让用户选择文本 user-select: none; 手机网页点击输入框的瞬间会出现灰色背景 解决方案: -webkit-tap-high ...
- int string convert
C++ int与string的转化 int本身也要用一串字符表示,前后没有双引号,告诉编译器把它当作一个数解释.缺省 情况下,是当成10进制(dec)来解释,如果想用8进制,16进制,怎么办?加上前缀 ...
- linux c数据库备份第四版
该版本算是比较成熟的啦,欢迎大伙拿来试用!!!1.新增数据库连接和备份时间配置文件conf2.新增日志文件,程序运行的一些异常会记录在log文件下 后续的工作:1.将代码切割为多个文件,分类存放代码2 ...
- 腾讯QQ、新浪微博等知名社交网络图标素材
腾讯QQ.新浪微博.QQ空间.淘宝.知乎.支付宝.大众点评等设计网络图标矢量素材. 注意是矢量素材,AI格式.放大缩小不变形. 社交网络图标几乎在网页制作中都会用到.一个好看的图标,完全可以提升整体网 ...
- JS 输出与变量
1. JS的输出 innerHTML: <!DOCTYPE html> <html> <head> <meta charset="utf-8&quo ...
- java 双重检查模式
java 双重检查模式 在并发环境下 兼顾安全和效率 成例(Idiom)是一种代码层次上的模式,是在比设计模式的层次更具体的层次上的代码技巧.成例往往与编程语言密切相关.双重检查成例(Double C ...
- 业内人士详述SIEM建设的演进过程
http://www.verydemo.com/demo_c289_i22006.html 4A http://www.verydemo.com/demo_c281_i40888.html 从SIEM ...