一、迭代器(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)详解的更多相关文章

  1. [转载]Java迭代器(iterator详解以及和for循环的区别)

    Java迭代器(iterator详解以及和for循环的区别) 觉得有用的话,欢迎一起讨论相互学习~[Follow] 转载自 https://blog.csdn.net/Jae_Wang/article ...

  2. python——iterator迭代器|iterator详解——20140918|

    -----------------------------------------------------------------------------前言--------------------- ...

  3. STL bind1st bind2nd详解

    STL bind1st bind2nd详解   先不要被吓到,其实这两个配接器很简单.首先,他们都在头文件<functional>中定义.其次,bind就是绑定的意思,而1st就代表fir ...

  4. C++ STL bitset 容器详解

    C++ STL bitset 容器详解 本篇随笔讲解\(C++STL\)中\(bitset\)容器的用法及常见使用技巧. \(bitset\)容器概论 \(bitset\)容器其实就是个\(01\)串 ...

  5. Qt迭代器(Java类型和STL类型)详解

    迭代器为访问容器类里的数据项提供了统一的方法,Qt 有两种迭代器类:Java 类型的迭代器和 STL 类型的迭代器. 两者比较,Java 类型的迭代器更易于使用,且提供一些高级功能,而 STL 类型的 ...

  6. 2.3 C++STL vector容器详解

    文章目录 2.3.1 引入 2.3.2 代码实例 2.3.3 运行结果 总结 2.3.1 引入 vector 容器 动态数组 可变数组 vector容器 单口容器(尾部操作效率高) vector动态增 ...

  7. STL 迭代器 iterator const

    STL迭代器很多时候可以当成指针来使用. 但是指针一般可以用const来控制访问. 那迭代器呢. #include <iostream> #include <vector> u ...

  8. Java集合之ArrayList和LinkedList的实现原理以及Iterator详解

    ArrayList实现可变数组的原理: 当元素超出数组内容,会产生一个新数组,将原来数组的数据复制到新数组中,再将新的元素添加到新数组中. ArrayList:是按照原数组的50%来延长,构造一个初始 ...

  9. Set,Multiset,Iterator(迭代器)详解

    Set,Multiset,Iterator(迭代器) Iterator:迭代器 我们可以发现所谓一些数据结构比如说数组和链表,它们都有一些相似的性质.我们看下面两个例子: 数组:定义数组\(int~a ...

  10. [GeekBand] STL 仿函数入门详解

    本文参考文献::GeekBand课堂内容,授课老师:张文杰 :C++ Primer 11 中文版(第五版) page 37 :网络资料: 叶卡同学的部落格  http://www.leavesite. ...

随机推荐

  1. HDU 4948 (傻比图论)

    Kingdom Problem Description Teacher Mai has a kingdom consisting of n cities. He has planned the tra ...

  2. HDU 5643 King's Game 【约瑟夫环】

    题意: 变形的约瑟夫环,最初为每个人编号1到n,第i次删去报号为i的人,然后从它的下一个人开始重新从1开始报号,问最终剩下第几号人? 分析: 首先看一下裸的约瑟夫环问题: 共n个人,从1开始报数,报到 ...

  3. 作为一名CEO

    你 不能怕得罪人 不能奢望完成工作的时候 有太多的愉悦感 你 必须要去做左右为难但左右亦可的 操蛋决策 你 得脸皮够厚 肚囊儿宽超 什么事情都能快速消化 哪怕 是 一坨屎 你 还得 决不放弃 永不言败 ...

  4. Vue中-下拉框可以选择可以填写

    <el-form-item label="方法名称"> <el-autocomplete popper-class="my-autocomplete&q ...

  5. C# .NET想要另存一个项目,sln文件丢了怎么办

    如下图所示,我想要另存一个工程,把 V4.4整个的项目另存为V4.5,我可以把解决方案文件(.sln)改名字,但是我没法把文件夹改名字,改了打开sln就说找不到.   很简单的一个思路是反正sln是多 ...

  6. HDU 3280 Equal Sum Partitions(二分查找)

    Equal Sum Partitions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  7. ym——Android怎样支持多种屏幕

    转载请注明本文出自Cym的博客(http://blog.csdn.net/cym492224103),谢谢支持! 原文链接:http://developer.android.com/guide/pra ...

  8. div拖拽缩放jquery插件编写——带8个控制点

    项目中需要对div进行拖拽缩放,需要有控制面板8个控制点的那种,原以为这么常见的效果应该能搜索到很多相关插件,然而可以完成拖拽的实繁,却找不到我想要的,还是自己动手丰衣足食吧 效果预览(只支持pc端) ...

  9. vue全局注册与局部注册的写法

    vue全局注册是每个实例化的vue都可以使用,而局部则是实例化注册的那个可以用.举个例子,看看写法: <div id="app"> <p>页面载入时,inp ...

  10. 演练:我的第一个 WPF 桌面应用程序 https://docs.microsoft.com/zh-cn/dotnet/framework/wpf/getting-started/walkthrough-my-first-wpf-desktop-application

    这篇文章演示如何开发简单的 Windows Presentation Foundation (WPF) 应用程序包括元素所共有的大多数 WPF 应用程序: 可扩展应用程序标记语言 (XAML) 标记. ...