`
bulote
  • 浏览: 1306699 次
文章分类
社区版块
存档分类
最新评论

Chapter 8 Visitation: Iterators and Containers

 
阅读更多
Chapter 8 Visitation: Iterators and Containers
tseraodnCin
• 8.1 Visitation
• 8.2 Iterators
• 8.3 An Example: quicksort()
• 8.4 Friendly Classes and Iterators
• 8.5 Generic Programming with void*
• 8.6 List and fist Iterator
• 8.7 Pragmatics
• Summary
• Exercises
Chapter 8 Visitation:
Iterators
tIseraotr
and Containers
tseraodnCin
• Container classes
C on t ai n e r c l as s e s
are used to hold a large
number of individual items.
• Many of the operations on container classes
involve the ability to visit individual
elements conveniently.
Chapter 8 Visitation:
Iterators
tIseraotr
and Containers
tseraodnCin
• In this chapter we explore a variety of
techniques to perform visitation and the
extraction of elements from a class. One
technique is to create an iterator whose
function is to visit the elements of an object
in a container class.
• Using visitation as the theme, we will write
code that anticipates using the container
classes and iterator objects and algorithms
found in the standard template library.
8.1 Visitation
• Conventional programming uses the for
statement as its preferred means of
structuring iteration, especially when
processing arrays.
8.1 Visitation
• Abstractly the computation is:
until no new elements
u n t i l n o n e w e l e m e n t s
sum += next element
n e xt e l e m e n t
• Demo on page 256: vector.h
– class vector
– boundaries of given vector
8.1 Visitation
8.1 Visitation
• Sample code
8.2 Iterators
• The pointer object and iterator visitation
idiom makes it very easy to write many
standard algorithms over container classes,
such as class vector.
• Demo on page 257: vector.h
– operator<<
– operator>>
8.2 Iterators
8.2 Iterators
• Demo on page 258: vectacum.cpp
– accumulate– sum the container values
8.3 An Example: quicksort()
• The quicksort
qu i c ks or t
sorting procedure, invented
by Anthony Hoare, is a highly efficient
internal sort.
• The qui cksort algorithm works by
recursively decomposing unordered values
into two subsets separated by a mid-value.
The mid-value is larger than all elements in
the first set and smaller than or equal to all
elements in the second set.
递归(Recursion)
-快速排序Quicksort
• 快速排序思路: (developed by C.A.R. Hoare in1962)
选中一个数组的元素,把数组分为两个子序
列,该元素前的序列中的元素都比它小,该
元素后的序列中的元素都比它大。以此类推
再将前后序列进行快速排序,直到序列中的
元素小于2个。
Demo qsort.c
递归
-Quicksort
• 快速排序思路: Idea of Quicksort
(1)swap(v,left,(left+right)/2);
left
(left+right)/2
right
递归
-Quicksort
• 快速排序思路: Idea of Quicksort
(2)partition
left
last
right
++last
4.10 递归
-Quicksort
• 快速排序思路: Idea of Quicksort
(3)swap(v,left,last);/*restore partion elem*/
left
last
right
4.10 递归
-Quicksort
• 快速排序思路: Idea of Quicksort
(3)qsort(v,left,last-1);(4)qsort(v,last+1,right)
left
last
right
last+1
last-1
8.3 An Example: quicksort()
• Demo on page 259: vectsort.cpp
– quicksort
– vector::iterator
v e c t o r : i t e r a t o r
partition
p a r t i t i o n
8.3 An Example: quicksort()
8.3 An Example: quicksort()
8.4 Friendly Classes and Iterators
• A class can define objects that are needed as
internal detail for other classes. A class that
handles these private objects must be on
friendly terms with them.
• Recall the mymystri
ng
type defined with
reference counting semantics in Section
6.10, "Strings Using Reference Semantics,"
on page 197.
– my_string
– str_obj
8.4 Friendly Classes and Iterators
• Demo on page 262: string8.h
– class str_obj
8.4 Friendly Classes and Iterators
• Iterators also typically need friendly
relations to the object they are visiting. Let
us add an iterator class and modify our
my_string to overload assignment and the
put to operator filling out this example.
• Demo on page 263: string8.h
– class my_string
– operator<<
8.4 Friendly Classes and Iterators
8.4 Friendly Classes and Iterators
– ostream& operator<<(ostream&, const my_
string&) requires friendly relations to str_obj.
– It cannot
c a n o t
be written as a member function
because its first argument must be an ostream.
– Since its return value is an ostream&, it can be
used in a multiple put to expression.
– It is worth pointing out that were not
assignment overloaded, the default assignment
semantics would fail.
8.4 Friendly Classes and Iterators
– We keep a private position variable cur_ind.
– Since the friendship relation between my_string
and str_ob j is not transitive,
• Demo on page 264: string8.h
– string_iterator, successor() member function
8.4 Friendly Classes and Iterators
8.4 Friendly Classes and Iterators
• Demo on page 264: string8.cpp
– word
8.5 Generic Programming with
void*
• The pointer type void* serves as a generic
or universal pointer type. Any other pointer
type can be assigned to it.
• We can see this in the definition of the
standard memory copying function memcpy.
• Demo on page 265: memcopy.cpp
8.5 Generic Programming with
void*
8.5 Generic Programming with
void*
• Demo on page 266: genstack.h
– class stack
8.5 Generic Programming with
void*
• Demo on page 266: month.cpp
8.6 List and fist Iterator
• In this section we develop a doubly linked
list whose interface is similar to the STL
library type list.
• We will give a basic design of this class and
its associated iterator definitions.
• Demo on page 267: list2.h
8.6 List and fist Iterator
8.6 List and fist Iterator
– We have struct 1istelem, which has a data
member and pointers to the next and previous
list elements.
– In our design, we add an iterator class nested
inside the list class. This class list::iterator will
be used to point at a current position inside a
list.
8.6 List and fist Iterator
• Demo on page 268: list2.h
– class iterator
8.6 List and fist Iterator
• Demo on page 268: list2.h
– operator++
– operator<<
8.6 List and fist Iterator
• Demo on page 268: list2.h
– The apparent dereferencing of p is
accomplished by accessing the data member of
the appropriate 1istelem using
1ist::iterator::operator*().
– The STL member functions for list are very
extensive; we will only show the code for a
representative set and will place several more in
the exercises.
8.6 List and fist Iterator
• Demo on page 268: list2.h
– push_front(char c)
8.6 List and fist Iterator
• Demo on page 268: list2.h
– list constructors
– The default constructor producing an empty list
has already been discussed. The second
constructor produces a list of n elements, all
initialized with a data member passed in as c.
8.6 List and fist Iterator
• Demo on page 268: list2.h
– list constructors
8.6 List and fist Iterator
• Demo on page 268: list2.h
– list destructor
8.7 Pragmatics
• Notice how the class list::iterator
mimics
definitions that are appropriate for post- and
pre- autoincrement and autodecrement.
• Demo on page list2.h:
– post- and pre- increment and decrement
8.7 Pragmatics
Summary
• 1. Visitation of the elements of an aggregate is a
fundamental operation.
• 2. Containers should designate a begin()
and an
end()
member returning an iterator.
• 3.
The pointer type void
*
serves as a generic or
universal pointer type.
• 4. We developed a doubly linked list whose
interface is similar to the STL library type list.
• 5. Iterators are either pointers or pointer-like
objects.
分享到:
评论

相关推荐

    Visitation

    Visitation

    Automata Theory and its Applications

    Zealand for reading and commenting on the chapter on Finite Automata. We also thank Michael Dinneen who has been involved in work related to update games and finite automata with equations. We thank ...

    scelta :(实验性)用于变体和可选类型的语法糖

    塞尔塔 variant和optional C ++ 17零开销语法糖。 目录 总览 std::variant和std::optional被引入C ++ 17的标准库... 为optional类型(包括infix语法)提供诸如map和and_then类的单子运算。 实施独立 scelta可以通过以下

    node-v9.6.0-x86.msi

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。

    Python基于机器学习的分布式系统故障诊断系统源代码,分布式系统的故障数据进行分析,设计故障诊断模型,高效地分析并识别故障类别

    基于技术手段(包括但不限于机器学习、深度学习等技术)对分布式系统的故障数据进行分析,设计故障诊断模型,高效地分析并识别故障类别,实现分布式系统故障运维的智能化,快速恢复故障的同时大大降低分布式系统运维工作的难度,减少运维对人力资源的消耗。在分布式系统中某个节点发生故障时,故障会沿着分布式系统的拓扑结构进行传播,造成自身节点及其邻接节点相关的KPI指标和发生大量日志异常

    JavaScript前端开发的核心语言前端开发的核心语言

    javascript 当今互联网时代,JavaScript已经成为了前端开发的核心语言它是一种高级程序设计语言,通常用于网页的交互和动态效果的实现。JavaScript的灵活性以及广泛的使用使得它变得异常重要,能够为用户带来更好的用户体验。 JavaScript的特点之一是它的轻量级,它可以在网页中运行无需单独的编译或下载。这意味着网页可以更快地加载并且用户无需安装额外的软件才能运行网页上的JavaScript代码。此外,与HTML和CSS紧密结合,可以直接在HTML文档中嵌入,使得网页的开发变得非常便捷。 JavaScript具有动态性,它可以在浏览器中实时修改页面内容和样。它可以通过操作DOM(文档对象模型来动态地修改网页的结构和布局,并且可以根据用户的行为实时地响应各种事件,如点击、标悬停、滚动等。这使得开发者可以轻松地为网页添加交互性和动态效果,提供更好的用户体验。 JavaScript也是一种面向对象的语言。它支持对象、类、继承、多态等面向对象编程的概念,使得代码结构更加清晰和可维护。开发者可以创建自定义的对象和方法,对功能进行封装和复用,提高代码的可读性和可维护性。

    四则运算自动生成程序安装包

    四则运算自动生成程序安装包

    基于Linux的私有文件服务器(网盘).zip

    基于Linux的私有文件服务器(网盘)

    源代码-access 管理 系统 API 文件.zip

    源代码-access 管理 系统 API 文件.zip

    海康机器人智能读码器工业协议操作手册V1.0.2.pdf

    海康机器人智能读码器工业协议操作手册V1.0.2.pdf

    256ssm-mysql-jsp 在线捐赠系统.zip(可运行源码+数据库文件+文档)

    根据需求,确定系统采用JSP技术,JAVA作为编程语言,MySQL作为数据库。整个系统要操作方便、易于维护、灵活实用。主要实现了系统用户管理、注册用户管理、信息发布管理、医疗物品分类管理、项目信息管理、捐赠项目管理、志愿者申请管理、个人求助管理、个人捐赠统计、系统管理等功能。 前台用户模块包括: 1. 首页:网站打开的第一个页面,显示网站的最新信息。 2. 用户注册/登录、3. 新闻资讯、4. 暖心故事、5. 我要求助、6. 我要捐赠、7. 我们的项目、8. 志愿者中心:实现志愿者中心的列表显示、9. 系统简介、10. 在线留言、11. 用户后台 后台管理员模块包括: 1. 系统用户管理、2. 注册用户管理、3. 信息发布管理、4. 医疗物品分类管理、5. 项目信息管理、6. 捐赠项目管理、7. 志愿者申请管理、8. 个人求助管理:管理员可以设置个人求助审核状态,可以删除个人求助审核信息。 9. 个人捐赠统计:管理员可以查看个人捐赠统计信息。 10. 系统管理:管理员可以对留言板信息进行查看、回复或删除 关键词:医药捐赠系统;JSP;MySQL

    系统性文献综述的撰写(Systematic Review)-范文2.pdf

    参考范文:Findings on Teaching Machine Learning inHigh School: A Ten -Year SystematicLiterature Review 用于学习研究,侵权请联系本人删除

    管理后台项目开发脚手架,基于vue-element-admin和springboot搭建,前后端分离方式开发和部署.zip

    管理后台项目开发脚手架,基于vue-element-admin和springboot搭建,前后端分离方式开发和部署.zip

    中世纪童话主题游戏设计元素组件素材Complete Fantasy Game UI kit.zip

    游戏开发资源,游戏UI,游戏GUI,游戏图标,PSD格式,XD格式,PNG下载,源文件,可编辑下载,游戏购物充值界面,宝石,图标,PS格式,AI格式等,游戏APP

    node-v7.7.3-sunos-x86.tar.gz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。

    毕业设计:Python图书馆大数据可视化分析系统(源码 + 数据库 + 说明文档)

    毕业设计:Python图书馆大数据可视化分析系统(源码 + 数据库 + 说明文档) 2 开发技术简介 4 2.1 基于B/S结构开发 4 2.2 python语言简介 4 2.3 MySQL数据库 4 3 需求分析 6 3.1 需求概述 6 3.2 业务流程分析 6 3.3 功能需求分析 7 3.4 性能需求分析 7 4 系统设计 8 4.1 设计指导思想和原则 8 4.2 界面设计 8 4.3 输入输出设计 9 4.4 数据库设计原则 9 4.5数据表设计 10 4.6系统模块总体设计 11 5 系统详细设计 12 5.1 注册 12 5.2 登录 13 5.3 图书列表 13 5.4 图书管理 14 6 系统测试 15 6.1 系统测试的方法与步骤 15 6.2 模块测试 15 6.4 评价 17

    什么是python-对于我们来说学习python的意义是什么

    python

    基于nwjs的网易云音乐Linux版客户端.zip

    基于nwjs的网易云音乐Linux版客户端

    node-v10.13.0-win-x86.zip

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。

    node-v8.11.2-x86.msi

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。

Global site tag (gtag.js) - Google Analytics