[C++ STL] 迭代器(iterator)详解
一、迭代器(iterator)介绍
指针可以用来遍历存储空间连续的数据结构,但是对于存储空间非连续的,就需要寻找一个行为类似指针的类,来对非数组的数据结构进行遍历。因此,我们引入迭代器概念。
迭代器(Iterator)是一种检查容器内元素并遍历元素的数据类型。迭代器是指针的泛化,它允许程序员用相同的方式处理不同的数据结构(容器)。
1、头文件
所有容器有含有其各自的迭代器型别(iterator types),所以当你使用一般的容器迭代器时,并不需要含入专门的头文件。不过有几种特别的迭代器,例如逆向迭代器,被定义于 <iterator> 中。
2 迭代器类型
迭代器共分为五种,分别为: 输入迭代器(Input iterator)、输出迭代器(Output iterator)、前向迭代器(Forward iterator)、双向迭代器(Bidirectional iterator)、随机存取迭代器(Random access iterator)。
二、容器迭代器的使用
下面列举了些例子说明各个容器的用法:
1、vector
#include <iostream>
#include <vector>
using namespace std;
int main(int argc, char* argv[])
{
// Create and populate the vector
vector<int> vecTemp;
for (int i = 0; i<6; i++)
{
vecTemp.push_back(i);
}
// Display contents of vector
cout <<"Original deque: ";
vector<int>::iterator it;
for (it = vecTemp.begin(); it!=vecTemp.end(); it++)
{
cout <<*it <<" ";
}
return 0;
}
/*
输出结果:
Original deque: 0 1 2 3 4 5
*/
2、deque
#include <iostream>
#include <deque>
using namespace std;
int main(int argc, char* argv[])
{
// Create and populate the deque
deque<int> dequeTemp;
for (int i = 0; i<6; i++)
{
dequeTemp.push_back(i);
}
// Display contents of deque
cout <<"Original deque: ";
deque<int>::iterator it;
for (it = dequeTemp.begin(); it != dequeTemp.end(); it++)
{
cout <<*it <<" ";
}
cout <<endl;
return 0;
}
/*
输出结果:
Original deque: 0 1 2 3 4 5
*/
3、list
#include <iostream>
#include <list>
using namespace std;
int main(int argc, char* argv[])
{
// Create and populate the list
list<int> listTemp;
for (int i = 0; i<6; i++)
{
listTemp.push_back(i);
}
// Display contents of list
cout << "Original list: ";
list<int>::iterator it;
for (it = listTemp.begin(); it != listTemp.end(); it++)
{
cout << *it << " ";
}
cout << endl;
// Insert five 9 into the list
list<int>::iterator itStart = listTemp.begin();
listTemp.insert(itStart,5,9);
// Display the result
cout << "Result of list: ";
for (it = listTemp.begin(); it != listTemp.end(); it++)
{
cout << *it << " ";
}
cout << endl;
return 0;
}
/*
输出结果:
Original list: 0 1 2 3 4 5
Result of list: 9 9 9 9 9 0 1 2 3 4 5
*/
4、set
#include <iostream>
#include <set>
using namespace std;
int main(int argc, char* argv[])
{
// Create and populate the set
set<char> setTemp;
for (int i = 0; i<6; i++)
{
setTemp.insert('F'-i);
}
// Display contents of set
cout <<"Original set: ";
set<char>::iterator it;
for (it = setTemp.begin(); it != setTemp.end(); it++)
{
cout <<*it <<" ";
}
cout <<endl;
return 0;
}
/*
输出结果:
Original set: A B C D E F
*/
5、map
#include <iostream>
#include <map>
using namespace std;
typedef map<int, char> MyMap;
int main(int argc, char* argv[])
{
// Create and populate the map
MyMap mapTemp;
for (int i = 0; i<6; i++)
{
mapTemp[i] = ('F'-i);
}
// Display contents of map
cout <<"Original map: " <<endl;
MyMap::iterator it;
for (it = mapTemp.begin(); it != mapTemp.end(); it++)
{
cout << (*it).first << " --> ";
cout << (*it).second << std::endl;
}
cout <<endl;
return 0;
}
/*
输出结果:
Original map:
0 --> F
1 --> E
2 --> D
3 --> C
4 --> B
5 --> A
*/
[C++ STL] 迭代器(iterator)详解的更多相关文章
- [转载]Java迭代器(iterator详解以及和for循环的区别)
Java迭代器(iterator详解以及和for循环的区别) 觉得有用的话,欢迎一起讨论相互学习~[Follow] 转载自 https://blog.csdn.net/Jae_Wang/article ...
- python——iterator迭代器|iterator详解——20140918|
-----------------------------------------------------------------------------前言--------------------- ...
- STL bind1st bind2nd详解
STL bind1st bind2nd详解 先不要被吓到,其实这两个配接器很简单.首先,他们都在头文件<functional>中定义.其次,bind就是绑定的意思,而1st就代表fir ...
- C++ STL bitset 容器详解
C++ STL bitset 容器详解 本篇随笔讲解\(C++STL\)中\(bitset\)容器的用法及常见使用技巧. \(bitset\)容器概论 \(bitset\)容器其实就是个\(01\)串 ...
- Qt迭代器(Java类型和STL类型)详解
迭代器为访问容器类里的数据项提供了统一的方法,Qt 有两种迭代器类:Java 类型的迭代器和 STL 类型的迭代器. 两者比较,Java 类型的迭代器更易于使用,且提供一些高级功能,而 STL 类型的 ...
- 2.3 C++STL vector容器详解
文章目录 2.3.1 引入 2.3.2 代码实例 2.3.3 运行结果 总结 2.3.1 引入 vector 容器 动态数组 可变数组 vector容器 单口容器(尾部操作效率高) vector动态增 ...
- STL 迭代器 iterator const
STL迭代器很多时候可以当成指针来使用. 但是指针一般可以用const来控制访问. 那迭代器呢. #include <iostream> #include <vector> u ...
- Java集合之ArrayList和LinkedList的实现原理以及Iterator详解
ArrayList实现可变数组的原理: 当元素超出数组内容,会产生一个新数组,将原来数组的数据复制到新数组中,再将新的元素添加到新数组中. ArrayList:是按照原数组的50%来延长,构造一个初始 ...
- Set,Multiset,Iterator(迭代器)详解
Set,Multiset,Iterator(迭代器) Iterator:迭代器 我们可以发现所谓一些数据结构比如说数组和链表,它们都有一些相似的性质.我们看下面两个例子: 数组:定义数组\(int~a ...
- [GeekBand] STL 仿函数入门详解
本文参考文献::GeekBand课堂内容,授课老师:张文杰 :C++ Primer 11 中文版(第五版) page 37 :网络资料: 叶卡同学的部落格 http://www.leavesite. ...
随机推荐
- HDU 3749 Financial Crisis(点-双连通分量)
Because of the financial crisis, a large number of enterprises go bankrupt. In addition to this, oth ...
- 餐巾(cogs 461)
[问题描述] 一个餐厅在相继的N天里,第i天需要Ri块餐巾(i=l,2,…,N).餐厅可以从三种途径获得餐巾. (1)购买新的餐巾,每块需p分: (2)把用过的餐巾送到快洗部,洗一块需m天,费用需f分 ...
- Mysql UPDATE 操作时含 Limit 注意事项
在update时,可以使用limit来设置,更新的条数,但下面这句sql语句是错误的. LIMIT ,; //错误提示:ERROR 1064 (42000): You have an error in ...
- redis可视化界面的操作【二十一】
1.安装 2.linux服务器中开启linux服务 root@qiaozhi:~# cd /usr/local/redis root@qiaozhi:/usr/local/redis# ./bin/ ...
- Memory Ordering in Modern Microprocessors
Linux has supported a large number of SMP systems based on a variety of CPUs since the 2.0 kernel. L ...
- maven的安装与环境变量配置
1.下载maven 地址:http://maven.apache.org/download.cgi 点击下载 apache-maven-3.2.1-bin.zip. 2.安装配置,假设maven 解压 ...
- Ubuntu 16.04安装unrar解压RAR文件
除了7zip:http://www.cnblogs.com/EasonJim/p/7124306.html之外,还可以安装unrar进行解压RAR文件. 安装 sudo apt-get install ...
- [Jexus系列] 一、安装并运行 Jexus
注意,本教程使用的jexus版本为5.8.3专业版,操作系统为 Ubunutu 16.04 64位 一.创建默认站点 不熟悉vim的可以看这个: vim超简单入门教程 sudo mkdir -p /v ...
- Redis源代码分析(六)--- ziplist压缩列表
ziplist和之前我解析过的adlist列表名字看上去的非常像.可是作用却全然不同.之前的adlist主要针对的是普通的数据链表操作. 而今天的ziplist指的是压缩链表.为什么叫压缩链表呢.由于 ...
- jquery.validate自己定义验证--成功提示与择要提示
1. 自己定义验证--成功提示 1) 加入选项 errorClass: "unchecked". validClass: "checked", errorEle ...