一、迭代器(iterator)介绍

指针可以用来遍历存储空间连续的数据结构,但是对于存储空间非连续的,就需要寻找一个行为类似指针的类,来对非数组的数据结构进行遍历。因此,我们引入迭代器概念。

迭代器(Iterator)是一种检查容器内元素并遍历元素的数据类型。迭代器是指针的泛化,它允许程序员用相同的方式处理不同的数据结构(容器)。

1、头文件

所有容器有含有其各自的迭代器型别(iterator types),所以当你使用一般的容器迭代器时,并不需要含入专门的头文件。不过有几种特别的迭代器,例如逆向迭代器,被定义于 <iterator> 中。

2 迭代器类型

迭代器共分为五种,分别为: 输入迭代器(Input iterator)、输出迭代器(Output iterator)、前向迭代器(Forward iterator)、双向迭代器(Bidirectional iterator)、随机存取迭代器(Random access iterator)。

二、容器迭代器的使用

下面列举了些例子说明各个容器的用法:

1、vector

  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. int main(int argc, char* argv[])
  5. {
  6. // Create and populate the vector
  7. vector<int> vecTemp;
  8. for (int i = 0; i<6; i++)
  9. {
  10. vecTemp.push_back(i);
  11. }
  12. // Display contents of vector
  13. cout <<"Original deque: ";
  14. vector<int>::iterator it;
  15. for (it = vecTemp.begin(); it!=vecTemp.end(); it++)
  16. {
  17. cout <<*it <<" ";
  18. }
  19. return 0;
  20. }
  21. /*
  22. 输出结果:
  23. Original deque: 0 1 2 3 4 5
  24. */

2、deque

  1. #include <iostream>
  2. #include <deque>
  3. using namespace std;
  4. int main(int argc, char* argv[])
  5. {
  6. // Create and populate the deque
  7. deque<int> dequeTemp;
  8. for (int i = 0; i<6; i++)
  9. {
  10. dequeTemp.push_back(i);
  11. }
  12. // Display contents of deque
  13. cout <<"Original deque: ";
  14. deque<int>::iterator it;
  15. for (it = dequeTemp.begin(); it != dequeTemp.end(); it++)
  16. {
  17. cout <<*it <<" ";
  18. }
  19. cout <<endl;
  20. return 0;
  21. }
  22. /*
  23. 输出结果:
  24. Original deque: 0 1 2 3 4 5
  25. */

3、list

  1. #include <iostream>
  2. #include <list>
  3. using namespace std;
  4. int main(int argc, char* argv[])
  5. {
  6. // Create and populate the list
  7. list<int> listTemp;
  8. for (int i = 0; i<6; i++)
  9. {
  10. listTemp.push_back(i);
  11. }
  12. // Display contents of list
  13. cout << "Original list: ";
  14. list<int>::iterator it;
  15. for (it = listTemp.begin(); it != listTemp.end(); it++)
  16. {
  17. cout << *it << " ";
  18. }
  19. cout << endl;
  20. // Insert five 9 into the list
  21. list<int>::iterator itStart = listTemp.begin();
  22. listTemp.insert(itStart,5,9);
  23. // Display the result
  24. cout << "Result of list: ";
  25. for (it = listTemp.begin(); it != listTemp.end(); it++)
  26. {
  27. cout << *it << " ";
  28. }
  29. cout << endl;
  30. return 0;
  31. }
  32. /*
  33. 输出结果:
  34. Original list: 0 1 2 3 4 5
  35. Result of list: 9 9 9 9 9 0 1 2 3 4 5
  36. */

4、set

  1. #include <iostream>
  2. #include <set>
  3. using namespace std;
  4. int main(int argc, char* argv[])
  5. {
  6. // Create and populate the set
  7. set<char> setTemp;
  8. for (int i = 0; i<6; i++)
  9. {
  10. setTemp.insert('F'-i);
  11. }
  12. // Display contents of set
  13. cout <<"Original set: ";
  14. set<char>::iterator it;
  15. for (it = setTemp.begin(); it != setTemp.end(); it++)
  16. {
  17. cout <<*it <<" ";
  18. }
  19. cout <<endl;
  20. return 0;
  21. }
  22. /*
  23. 输出结果:
  24. Original set: A B C D E F
  25. */

5、map

  1. #include <iostream>
  2. #include <map>
  3. using namespace std;
  4. typedef map<int, char> MyMap;
  5. int main(int argc, char* argv[])
  6. {
  7. // Create and populate the map
  8. MyMap mapTemp;
  9. for (int i = 0; i<6; i++)
  10. {
  11. mapTemp[i] = ('F'-i);
  12. }
  13. // Display contents of map
  14. cout <<"Original map: " <<endl;
  15. MyMap::iterator it;
  16. for (it = mapTemp.begin(); it != mapTemp.end(); it++)
  17. {
  18. cout << (*it).first << " --> ";
  19. cout << (*it).second << std::endl;
  20. }
  21. cout <<endl;
  22. return 0;
  23. }
  24. /*
  25. 输出结果:
  26. Original map:
  27. 0 --> F
  28. 1 --> E
  29. 2 --> D
  30. 3 --> C
  31. 4 --> B
  32. 5 --> A
  33. */

[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. 洛谷 通天系列 P1760 P1757 P1759

    P1760 通天之汉诺塔 汉诺塔问题.一个高精乘单精解决 ans=2^n-1 /*by SilverN*/ #include<algorithm> #include<iostream ...

  2. Codeforces Gym100495 B、D、E、F、K

    http://codeforces.com/gym/100495 K题 草地的面积减去相交的面积,计算几何,垃圾题,避免不必要的计算损失精度(能约分的约分) 卡了老子一个星期了 再加前几天的一道题 这 ...

  3. POJ 2456_Aggressive cows

    题意: 给定N个位置,把C头牛分别放入,求相邻两头牛的最大距离. 分析: 即为求两头牛之间最小距离的最大值.二分搜索答案. 代码: #include<iostream> #include& ...

  4. iText输出中文的三种字体选择方式

    1.使用iTextAsian.jar中的字体    BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",Bas ...

  5. html上传图片类型

    <html>  <head>  <meta charset="utf-8">  <title>上传图片</title> ...

  6. SVG :可缩放矢量图形(Scalable Vector Graphics)。

    SVG 意为可缩放矢量图形(Scalable Vector Graphics). SVG 使用 XML 格式定义图像. SVG 图像在放大或改变尺寸的情况下其图形质量不会有所损失 SVG 使用 XML ...

  7. Android帧动画实现,防OOM,比原生动画集节约超过十倍的资源

    2015年项目接到一个需求,实现一个向导动画,这个动画一共六十张图片,当时使用的是全志A33的开发(512的内存),通过使用Android的动画集实现,效果特别卡顿,然后想到这样的方式来实现,效果非常 ...

  8. WindowFromPoint -- 获得包括指定点的窗体的句柄

     WindowFromPoint 函数功能: 该函数获得包括指定点的窗体的句柄. 函数原型: HWND WindowFromPoint(POINT Point): 參数: Point:指定一个被检 ...

  9. 利用栈Stack实现队列(Queue)

    实现说明: 入队时,将元素压入s1; 出队时,推断s2是否为空,如不为空,则直接弹出顶元素:如为空.则将s1的元素逐个"倒入"s2.把最后一个元素弹出并出队; 这个思路,避免了重复 ...

  10. jquery-mobile 学习笔记之中的一个(基础属性)

    写在前面 本文是依据w3c 学习轨迹,自己研习过程中记录下的笔记,仅仅供自己学习轨迹记录之用,不喜勿喷. 0 引入库 引入相应的文件: <link rel="stylesheet&qu ...