STL Iterators

时间:2023-01-18 19:22:52

Summary of Chapter 33 STL Iterators from The C++ Programming Language 4th. Ed., Bjarne Stroustrup.

-------------------------------------------------------------------------------------------------------------------------------------------------

The reason that STL containers and algorithms work so well together is that they now nothing of each other.

                                          -- Alex Stepanov

Iterators are the glue that ties standard-library algorithms to their data. Conversely, you can say that iterators are the mechanism used to minimize an algorithm's dependence on the data structures on which it operates:

Iterator Model

An iterator is akin to a pointer in that it provides operations for indirect access (e.g., * for dereferencing) and for moving to point to a new element (e.g., ++ for moving to the next element).

Iterator Categories

The standard library provides five kinds of iterators (five iterator categories):

  • Input iterator: We can iterate forward using ++ and read each element (repeatedly) using *. We can compare input interators using == and !=. This is the kind of iterator that istream offers;
  • Output iterator: We can iterate forward using ++ and write an element once only using *. That is the kind of iterator that ostream offers;
  • Forward iterator: We can iterate forward repeatedly using ++ and read and write (unless the elements are const) elements repeatedly using *. If a forward iterator points to a class object, we can use -> to refer to a member. We can compare forward iterators using == and !=. This is the kind of iterator forward_list offers.
  • Bidirectional iterator: We can iterate forward (using ++) and backward (using --) and read and write (unless the elements are const) elements repeatedly using *. If a bidirectional iterator points to a class object, we can use -> to refer to a member. We can compare bidirectional iterators using == and !=. This is the kind of iterator that list, map, and set offer.
  • Random-access iterator; we can iterate forward (using ++ or +=) and backward (using - or -=) and read and write (unless the elements are const) elements repeatedly using * or []. If a random-access iterator points to a class object, we can use -> to refer to a member. We can subscript a random-access iterator using [], add an integer using + and subtract an iteger using -. We can find the distance between two random-acess iterators to the same sequence by subtracting one from the other. We can compare random-access iterators using ==, !=, <, <=, >, and >=. This is the kind of iterator that vector offres.

If you need to do something advanced with iterator categories, use iterator_traints (directly or indirectly).

STL Iterators的更多相关文章

  1. STL源码--iterator和traits编程技法

    第一部分 iterator学习 STL iterators定义: 提供一种方法,使之能够依序巡访某个聚合物(容器)所含的各个元素,而又无需暴露该聚合物的内部表达方式. 任何iteartor都应该提供5 ...

  2. &lbrack;设计模式&rsqb; 16 迭代器模式 Iterator Pattern

    在GOF的<设计模式:可复用面向对象软件的基础>一书中对迭代器模式是这样说的:提供一种方法顺序访问一个聚合对象中各个元素,而又不需要暴露该对象的内部表示. 类图和实例: 迭代器模式由以下角 ...

  3. Iterator pattern(c&plus;&plus;实现)

    概述: 在现在的电视机中,我们使用[后一个]和[前一个]按钮可以很方便的换台,当按下[后一个]按钮时,将切换到下一个预置的频道.想象一下在陌生的城市中的旅店中看电视.当改变频道时,重要的不是几频道,而 ...

  4. Qt容器类(总结)(新发现的QQueue和QStack,注意全都是泛型)

    Introduction Qt库提供了一组基于模板的一般化的容器类.这些容器可以存储指定的类型的元素.例如,如果你需要一个可变大小的Qstring数组,可以用QVector<QString&gt ...

  5. 《C&plus;&plus;程序设计语言&lpar;英文第四版&rpar;》【PDF】下载

    <C++程序设计语言(英文第四版)>[PDF]下载链接: https://u253469.pipipan.com/fs/253469-230382177 内容简介 本书是C++领域经典的参 ...

  6. UVA 11988 Broken Keyboard &lpar;a&period;k&period;a&period; Beiju Text&rpar; (链表,模拟)

    使用list来模拟就行了,如果熟悉list,那么这道题真是分分钟秒掉... list是双向循环链表,插入和删除操作非常快,缺点是不能像数组一样随机按下标读取. 一下是wiki上说明的相关函数:http ...

  7. Understand the Qt containers(有对应表)

    Container classes are one of the cornerstones of object-oriented programming, invaluable tools that ...

  8. C&plus;&plus;学习书籍推荐《C&plus;&plus;标准库(第一版)》下载

    百度云及其他网盘下载地址:点我 编辑推荐 <C++标准程序库:自修教程与参考手册>编辑推荐:C++标准程序库提供了一组通用类别(classes)和界面(interfaes),可大幅扩充C+ ...

  9. c&plus;&plus;学习书籍推荐《The C&plus;&plus; Programming Language第四版》下载

    百度云及其他网盘下载地址:点我 作者简介 Bjarne Stroustrup is the designer and original implementer of C++, the author o ...

随机推荐

  1. &period;net 导出Excel功能

    将DataSet对象导出成Excel文档 一.不带格式控制 void btnExport_Click(object sender, EventArgs e) { IList<string> ...

  2. 虚拟机centos配置ip

    涉及到三个配置文件,分别是: /etc/sysconfig/network /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/resolv.conf /et ...

  3. 如何新建XCode项目

    一.IOS的基础知识 1.只有一个应用程序正在运行.在IOS上,每一段时间内只能激活一个应用程序并在屏幕上显示. 2.只有一个窗口.只允许应用程序操作的一个窗口. 3.访问受限.只能在IOS为应用程序 ...

  4. SSIS - 1&period;简介

    一.什么是SSIS? 1)SSIS全称为Microsoft SQL Server Integration Services. 2)ETL全称为Extraction, Transformation an ...

  5. Python3创建项目时创建了一个叫做&OpenCurlyDoubleQuote;keyword&quot&semi;的包,运行项目时报ImportError&colon; cannot import name &&num;39&semi;iskeyword&&num;39&semi;错误

    导致该问题的原因为在Python3中keyword是python的关键字包,所以在给包命名时应避免使用关键字进行命名.解决方法,将keword包名称修改为'keywords'就可以了.

  6. net core体系-web应用程序-1VS2017构建一个简单的web

    使用vs2017,添加一个新项目-asp.net core web应用程序. 结构如图, wwwroot放了网站的静态资源如css.js.image文件: appsetting.json是应用程序的配 ...

  7. osg shader 相机观察矩阵逆矩阵 求顶点世界坐标

    uniform mat4 osg_ViewMatrixInverse;//osg内置uniform void main() { vec4 posWorld = osg_ViewMatrixInvers ...

  8. MaC 修改MySQL密码

    1.苹果->系统偏好设置->最下边点mysql 在弹出页面中 关闭mysql服务(点击stop mysql server) 2.进入终端输入:cd /usr/local/mysql/bin ...

  9. redis搭建集群并用TreeSoft管理

    前言:redis作为一款高效的NOSQL数据库已经深入贯彻和落实到我们的日常开发代码中,作为缓存.时间控制.数据仓库.队列等使用方法层出不穷,简直是开写代码.居家旅行之必备良药.曾经,我们的项目都是单 ...

  10. 20145302张薇《Java程序设计》第十六周课程总结

    20145302 <Java程序设计>第十六周课程总结 实验报告链接汇总 实验一 Java开发环境的熟悉 实验二 Java面向对象程序设计 实验三 敏捷开发与XP实践 实验四 Andoid ...