logo资料库

CPP PRIMER.pdf

第1页 / 共1037页
第2页 / 共1037页
第3页 / 共1037页
第4页 / 共1037页
第5页 / 共1037页
第6页 / 共1037页
第7页 / 共1037页
第8页 / 共1037页
资料共1037页,剩余部分请下载后查看
封面
前言
第四版的改动
本书结构
致谢
第一章 快速入门
1.1. 编写简单的 C++ 程序
1.1.1. 编译与执行程序
程序源文件命名规范
调用 GNU 或微软编译器
从命令行编译器
Exercises Section 1.1.1
1.2. 初窥输入/输出
1.2.1. 标准输入与输出对象
1.2.2. 一个使用IO库的程序
写入到流
使用标准库中的名字
读入流
完成程序
关键概念:已初始化变量和未初始化变量
Exercises Section 1.2.2
1.3. 关于注释
注释对不可嵌套
1.4. 控制结构
1.4.1. while 语句
关键概念:C++ 程序的缩排和格式
1.4.2. for 语句
再谈编译
Exercises Section 1.4.2
1.4.3. if 语句
Exercises Section 1.4.3
1.4.4. 读入未知数目的输入
从键盘输入文件结束符
Exercises Section 1.4.4
1.5. 类的简介
1.5.1. Sales_item 类
Sales_item 对象上的操作
读入和写出 Sales_item 对象
关键概念:类定义行为
将 Sales_item 对象相加
Exercises Section 1.5.1
1.5.2. 初窥成员函数
什么是成员函数
Exercises Section 1.5.2
1.6. C++ 程序
Exercises Section 1.6
小结
第一部分 基本语言
第二章 变量和基本类型
2.1. 基本内置类型
表 2.1. C++ 算术类型
2.1.1. 整型
内置类型的机器级表示
带符号和无符号类型
整型值的表示
整型的赋值
2.1.2. 浮点型
建议:使用内置算术类型
Exercises Section 2.1.2
2.2. 字面值常量
整型字面值规则
浮点字面值规则
布尔字面值和字符字面值
非打印字符的转义序列
字符串字面值
字符串字面值的连接
多行字面值
建议:不要依赖未定义行为
Exercises Section 2.2
2.3. 变量
Exercises Section 2.3
关键概念:强静态类型
2.3.1. 什么是变量
左值和右值
Exercises Section 2.3.1
术语:什么是对象?
2.3.2. 变量名
C++ 关键字
表 2.2. C++ 关键字
表 2.3. C++ 操作符替代名
变量命名习惯
Exercises Section 2.3.2
2.3.3. 定义对象
初始化
使用多个初始化式
初始化多个变量
Exercises Section 2.3.3
2.3.4. 变量初始化规则
内置类型变量的初始化
警告:未初始化的变量引起运行问题
类类型变量的初始化
Exercises Section 2.3.4
2.3.5. 声明和定义
Exercises Section 2.3.5
2.3.6. 名字的作用域
C++ 中作用域可嵌套
2.3.7. 在变量使用处定义变量
Exercises Section 2.3.6
2.4. const 限定符
定义 const 对象
const 对象默认为文件的局部变量
Exercises Section 2.4
2.5. 引用
引用是别名
定义多个引用
const 引用
术语:const 引用是指向 const 的引用
Exercises Section 2.5
2.6. typedef 名字
2.7. 枚举
定义和初始化枚举
枚举成员是常量
每个 enum 都定义一种唯一的类型
2.8. 类类型
从操作开始设计类
定义 Sales_item 类
类的数据成员
访问标号
使用 struct 关键字
Exercises Section 2.8
2.9. 编写自己的头文件
2.9.1. 设计自己的头文件
编译和链接多个源文件
头文件用于声明而不是用于定义
一些 const 对象定义在头文件中
Exercises Section 2.9.1
2.9.2. 预处理器的简单介绍
头文件经常需要其他头文件
避免多重包含
使用自定义的头文件
小结
第三章 标准库类型
3.1. 命名空间的 using 声明
每个名字都需要一个 using 声明
使用标准库类型的类定义
Exercises Section 3.1
3.2. 标准库 string 类型
3.2.1. string 对象的定义和初始化
表 3.1. 几种初始化 string 对象的方式
警告:标准库 string 类型和字符串字面值
Exercises Section 3.2.1
3.2.2. string 对象的读写
读入未知数目的 string 对象
使用 getline 读取整行文本
Exercises Section 3.2.2
3.2.3. string 对象的操作
string 的 size 和 empty 操作
string::size_type 类型
string 关系操作符
string 对象的赋值
两个 string 对象相加
和字符串字面值的连接
从 string 对象获取字符
下标操作可用作左值
计算下标值
3.2.4. string 对象中字符的处理
表 3.3. cctype 中的函数
建议:采用 C 标准库头文件的 C++ 版本
Exercises Section 3.2.4
3.3. 标准库 vector 类型
3.3.1. vector 对象的定义和初始化
表 3.4. 初始化vector
创建确定个数的元素
关键概念:vector 对象动态增长
值初始化
Exercises Section 3.3.1
3.3.2. vector 对象的操作
表 3.5. vector 操作
vector 对象的 size
向 vector 添加元素
vector 的下标操作
下标操作不添加元素
关键概念:安全的泛型编程
警告:仅能对确知已存在的元素进行下标操作
Exercises Section 3.3.2
3.4. 迭代器简介
容器的 iterator 类型
术语:迭代器和迭代器类型
begin 和 end 操作
vector 迭代器的自增和解引用运算
迭代器的其他操作
迭代器应用的程序示例
Exercises Section 3.4
Exercises Section 3.4.1
3.5. 标准库 bitset 
表 3.6. 初始化 bitset 对象的方法
用 unsigned 值初始化 bitset 对象
用 string 对象初始化 bitset 对象
表 3.7. bitset 操作
测试整个 bitset 对象
访问 bitset 对象中的位
对整个 bitset 对象进行设置
获取 bitset 对象的值
输出二进制位
使用位操作符
Exercises Section 3.5.2
小结
第四章 数组和指针
4.1. 数组
显式初始化数组元素
特殊的字符数组
不允许数组直接复制和赋值
警告:数组的长度是固定的
Exercises Section 4.1.1
检查数组下标值
Exercises Section 4.1.2
4.2. 指针的引入
建议:尽量避免使用指针和数组
指针变量的定义
另一种声明指针的风格
连续声明多个指针易导致混淆
指针可能的取值
避免使用未初始化的指针
指针初始化和赋值操作的约束
void* 指针
Exercises Section 4.2.2
生成左值的解引用操作
关键概念:给指针赋值或通过指针进行赋值
指针和引用的比较
指向指针的指针
Exercises Section 4.2.3
指针的算术操作
解引用和指针算术操作之间的相互作用
下标和指针
计算数组的超出末端指针
输出数组元素
指针是数组的迭代器
Exercises Section 4.2.4
指向 const 对象的指针
const 指针
指向 const 对象的 const 指针
指针和 typedef
建议:理解复杂的 const 类型的声明
Exercises Section 4.3
4.3. C 风格字符串
表 4.1. 操纵 C 风格字符串的标准库函数
Exercises Section 4.3
动态数组的定义
初始化动态分配的数组
const 对象的动态数组
允许动态分配空数组
动态空间的释放
C 风格字符串与 C++ 的标准库类型 string 的比较
动态数组的使用
Exercises Section 4.3.1
混合使用标准库类 string 和 C 风格字符串
使用数组初始化 vector 对象
Exercises Section 4.3.2
4.4. 多维数组
用 typedef 简化指向多维数组的指针
Exercises Section 4.4.1
小结
第五章 表达式
5.1. 算术操作符
表 5.1. 算术操作符
警告:溢出和其他算术异常
Exercises Section 5.1
5.2. 关系操作符和逻辑操作符
表 5.2. 关系操作符和逻辑操作符
Exercises Section 5.2
5.3. 位操作符
表 5.3. 位操作符
Exercises Section 5.3.1
IO 操作符为左结合
5.4. 赋值操作符
谨防混淆相等操作符和赋值操作符
Exercises Section 5.4.2
Exercises Section 5.4.3
5.5. 自增和自减操作符
建议:只有在必要时才使用后置操作符
建议:简洁即是美
Exercises Section 5.5
5.6. 箭头操作符
Exercises Section 5.6
5.7. 条件操作符
Exercises Section 5.7
5.8. sizeof 操作符
Exercises Section 5.8
5.9. 逗号操作符
Exercises Section 5.9
5.10. 复合表达式的求值
圆括号凌驾于优先级之上
表 5.4. 操作符的优先级
Exercises Section 5.10.2
建议:复合表达式的处理
Exercises Section 5.10.3
5.11. new 和 delete 表达式
警告:动态内存的管理容易出错
Exercises Section 5.11
5.12. 类型转换
有符号与无符号类型之间的转换
理解算术转换
指针转换
转换为 bool 类型
算术类型与 bool 类型的转换
转换与枚举类型
转换为 const 对象
由标准库类型定义的转换
Exercises Section 5.12.3
建议:避免使用强制类型转换
Exercises Section 5.12.7
小结
第六章 语句
6.1. 简单语句
6.2. 声明语句
6.3. 复合语句(块)
Exercises Section 6.3
6.5. if 语句
Exercises Section 6.5.1
6.6. switch 语句
Exercises Section 6.6.5
Code for Exercises in Section 6.6.5
6.7. while 语句
Exercises Section 6.7
6.8. for 循环语句
Exercises Section 6.8.2
6.9. do while 语句
Exercises Section 6.9
6.10. break 语句
Exercises Section 6.10
6.11. continue 语句
Exercises Section 6.11
6.12. goto 语句
Exercises Section 6.12
6.13. try 块和异常处理
Exercises Section 6.13.2
6.14. 使用预处理器进行调试
Exercises Section 6.14
小结
第七章 函数
7.1. 函数的定义
Exercises Section 7.1.2
7.2. 参数传递
Exercises Section 7.2.1
Exercises Section 7.2.2
Exercises Section 7.2.5
Exercises Section 7.2.6
7.3. return 语句
Exercises Section 7.3.2
Exercises Section 7.3.3
7.4. 函数声明
Exercises Section 7.4
Exercises Section 7.4.1
7.5. 局部对象
Exercises Section 7.5.2
7.6. 内联函数
Exercises Section 7.6
7.7. 类的成员函数
Exercises Section 7.7.4
7.8. 重载函数
建议:何时不重载函数名
Exercises Section 7.8.1
Exercises Section 7.8.3
Exercises Section 7.8.4
7.9. 指向函数的指针
小结
第八章 标准 IO 库
8.1. 面向对象的标准库
Exercises Section 8.1
8.2. 条件状态
Exercises Section 8.2
8.3. 输出缓冲区的管理
警告:如果程序崩溃了,则不会刷新缓冲区
8.4. 文件的输入和输出
警告:C++ 中的文件名
Exercises Section 8.4.1
Exercises Section 8.4.3
8.5. 字符串流
Exercises Section 8.5
小结
第二部分:容器和算法
第九章. 顺序容器
9.1. 顺序容器的定义
Exercises Section 9.1.1
Exercises Section 9.1.2
9.2. 迭代器和迭代器范围
Exercises Section 9.2
对形成迭代器范围的迭代器的要求
Exercises Section 9.2.1
9.3. 每种顺序容器都提供了一组有用的类型定义以及以下操作:
Exercises Section 9.3.1
关键概念:容器元素都是副本
Exercises Section 9.3.3
Exercises Section 9.3.4
Exercises Section 9.3.5
Exercises Section 9.3.6
Exercises Section 9.3.7
Exercises Section 9.3.8
9.4. vector 容器的自增长
Exercises Section 9.4.1
9.5. 容器的选用
Exercises Section 9.5
小结
小结
小结
术语
第三部分:类和数据抽象
小结
14.1. 重载操作符的定义
习题 14.1
警告:审慎使用操作符重载
Exercises Section 14.1.1
14.2. 输入和输出操作符
Exercises Section 14.2.1
14.3. 算术操作符和关系操作符
Exercises Section 14.3
14.4. 赋值操作符
Exercises Section 14.4
14.5. 下标操作符
Exercises Section 14.5
14.6. 成员访问操作符
Exercises Section 14.6
14.7. 自增操作符和自减操作符
Exercises Section 14.7
14.8. 调用操作符和函数对象
Exercises Section 14.8
Exercises Section 14.8.1
14.9. 转换与类类型
Exercises Section 14.9.2
警告:避免转换函数的过度使用
Exercises Section 14.9.4
警告:转换和操作符
Exercises Section 14.9.5
小结
术语
第四部分:面向对象编程与泛型编程
15.1. 面向对象编程:概述
15.2. 定义基类和派生类
Exercises Section 15.2.1
关键概念:类设计与受保护成员
Exercises Section 15.2.3
关键概念:C++ 中的多态性
Exercises Section 15.2.4
关键概念:继承与组合
Exercises Section 15.2.5
Exercises Section 15.2.7
15.3. 转换与继承
15.4. 构造函数和复制控制
关键概念:重构
关键概念:尊重基类接口
Exercises Section 15.4.2
Exercises Section 15.4.4
15.5. 继承情况下的类作用域
Exercises Section 15.5.1
Exercises Section 15.5.2
15.6. 纯虚函数
Exercises Section 15.6
15.7. 容器与继承
Exercises Section 15.7
15.8. 句柄类与继承
Exercises Section 15.8.2
Exercises Section 15.8.3
15.9. 再谈文本查询示例
Exercises Section 15.9.2
Exercises Section 15.9.5
Exercises Section 15.9.6
小结
术语
16.1. 模板定义
Exercises Section 16.1.1
Exercises Section 16.1.2
Exercises Section 16.1.3
Exercises Section 16.1.4
Exercises Section 16.1.5
Exercises Section 16.1.6
警告:链接时的编译时错误
16.2. 实例化
Exercises Section 16.2.1
16.3. 模板编译模型
Exercises Section 16.3
警告:类模板中的名字查找
16.4. 类模板成员
Exercises Section 16.4
Exercises Section 16.4.1
Exercises Section 16.4.2
Exercises Section 16.4.4
Exercises Section 16.4.6
16.5. 一个泛型句柄类
Exercises Section 16.5.1
Exercises Section 16.5.2
16.6. 模板特化
Exercises Section 16.6.1
Exercises Section 16.6.2
Exercises Section 16.6.3
16.7. 重载与函数模板
Exercises Section 16.7
小结
术语
第五部分 高级主题
17.1. 异常处理
Exercises Section 17.1.1
Exercises Section 17.1.3
Exercises Section 17.1.5
Exercises Section 17.1.8
Exercises Section 17.1.9
警告:Auto_ptr 缺陷
Exercises Section 17.1.11
小结
术语
小结
A.1. 标准库名字和头文件
1
前言 本书全面介绍了 C++ 语言。作为一本入门书(Primer),它以教程的形式 对 C++ 语言进行清晰的讲解,并辅以丰富的示例和各种学习辅助手段。与大多 数入门教程不同,本书对 C++ 语言本身进行了详尽的描述,并特别着重介绍了 目前通行的、行之有效的程序设计技巧。 无数程序员曾使用本书的前几个版本学习 C++,在此期间 C++ 也逐渐发展 成熟。这些年来,C++ 语言的发展方向以及 C++ 程序员的关注点,已经从以往 注重运行时的效率,转到千方百计地提高程序员的编程效率上。随着标准库的广 泛可用,我们现在能够比以往任何时候更高效地学习和使用 C++。本书这一版本 充分体现了这一点。 第四版的改动 为了体现现代 C++ 编程风格,我们重新组织并重写了本书。书中不再强调 低层编程技术,而把中心转向标准库的使用。书中很早就开始介绍标准库,示例 也已经重新改写,充分利用了标准库设施。我们也对语言主题叙述的先后次序进 行了重新编排,使讲解更加流畅。 除重新组织内容外,为了便于读者理解,我们还增加了几个新的环节。每一 章都新增了“小结”和“术语”,概括本章要点。读者可以利用这些部分进行自 我检查;如果发现还有不理解的概念,可以重新学习该章中的相关部分。 书中还加入了下述几种学习辅助手段: • 重要术语用黑体表示,我们认为读者已经熟悉的重要术语则用楷体表示。 这些术语都会出现在的“术语”部分。 • 书中用特殊版式突出标注的文字,是为了向读者提醒语言的重要特征,警 示常见的错误,标明良好的编程实践,列出通用的使用技巧。希望这些标 注可以帮助读者更快地消化重要概念,避免犯常见错误。 • 为了更易于理解各种特征或概念间的关系,书中大量使用了前后交叉引 用。 • 对于某些重要概念和 C++ 新手最头疼的问题,我们进行了额外的讨论和 解释。这部分也以特殊版式标出。 • 学习任何程序设计语言都需要编写程序。因此,本提供了大量的示例。所 有示例的源代码可以从下列网址获得: • http://www.awprofessional.com/cpp_primer 万变不离其宗,本书保持了前几版的特色,仍然是一部全面介绍 C++ 的教程。 我们的目标是提供一本清晰、全面、准确的指南性读物。我们通过讲解一系列示 例来教授 C++ 语言,示例除了解释语言特征外,还展示了如何善用这门语言。 2
虽然读者不需要事先学过 C 语言(C++ 最初的基础)的知识,但我们假定读者 已经掌握了一种现代结构化语言。 本书结构 本介绍了 C++ 国际标准,既涵盖语言的特征,又讲述了也是标准组成部分 的丰富标准库。C++ 的强大很大程度上来自它支持抽象程序设计。要学会用 C++ 高效地编程,只是掌握句法和语义是远远不够的。我们的重点在于,教会读者怎 样利用 C++ 的特性,快速地写出安全的而且性能可与 C 语言低层程序相媲美的 程序。 C++ 是一种大型的编程语言,这可能会吓倒一些新手。现代 C++ 可以看成由 以下三部分组成: • 低级语言,多半继承自 C。 • 更高级的语言特征,用户可以借此定义自己的数据类型,组织大规模的程 序和系统。 • 标准库,使用上述高级特征提供一整套有用的数据结构和算法。 多数 C++ 教材按照下面的顺序展开:先讲低级细节,再介绍更高级的语言特 征;在讲完整个语言后才开始解释标准库。结果往往使读者纠缠于低级的程序设 计问题和复杂类型定义的编写等细节,而不能真正领会抽象编程的强大,更不用 说学到足够的知识去创建自己的抽象了。 本版中我们独辟蹊径。一开始就讲述语言的基础知识和标准库,这样读者就 可以写出比较大的有实际意义的程序来。透彻阐释了使用标准库(并且用标准库 编写了各种抽象程序)的基础知识之后,我们才进入下一步,学习用 C++ 的其 他高级特征来编写自己的抽象。 第一和第二部分讨论语言的基础知识和标准库设施。其重点在于学会如何编 写 C++ 程序,如何使用标准库提供的抽象设施。大部分 C++ 程序员需要了解本 书这两部分的内容。 除了讲解基础知识以外,这两部分还有另外一个重要的意图。标准库设施本 身是用 C++ 编写的抽象数据类型,定义标准库使用的是任何 C++ 程序员都能使 用的构造类的语言特征。我们教授 C++ 的经验说明,一开始就使用设计良好的 抽象类型,读者会更容易理解如何建立自己的类型。 第三到第五部分着重讨论如何编写自己的类型。第三部分介绍 C++ 的核心, 即对类的支持。类机制提供了编写自定义抽象的基础。类也是第四部分中讨论的 面向对象编程和泛型编程的基础。全书正文的最后是第五部分,这一部分讨论了 一些高级特征,它们在构建大型复杂系统时最为常用。 3
致谢 与前几版一新,我们要感谢 Bjarne Stroustrup,他不知疲倦地从事着 C++ 方面的工作,他与我们的深厚友情由来已久。我们还要感谢 Alex Stepanov,正 是他最初凭借敏锐的洞察力创造了容器和算法的概念,这些概念最终形成了标准 库的核心。此外,我们要感谢 C++ 标准委员会的所有成员,他们多年来为 C++ 澄 清概念、细化标准和改进功能付出了艰苦的努力。 我们要衷心地感谢本书审稿人,他们审阅了我们的多份书稿,帮助我们对本 书进行了无数大大小小的修改。他们是 Paul Abrahams,Michael Ball,Mary Dageforde,Paul DuBois,Matt Greenwood,Matthew P. Johnson,Andrew Koenig, Nevin Liber,Bill Locke,Robert Murray,Phil Romanik,Justin Shaw,Victor Shtern,Clovis Tondo,Daveed Vandevoorde 和 Steve Vinoski。 书中所有示例都已通过 GNU 和微软编译器的编译。感谢他们的开发者和所 有开发其他 C++ 编译器的人,是他们使 C++ 变成现实。 最后,感谢 的工作人员,他们引领了这一版的整个出版过程:——我们最 初的编辑,是他提出出版本书的新版,他从本书最初版本起就一直致力于本书; ——我们的新编辑,他坚持更新和精简本书内容,极大地改进了这一版本;—— 他保证了我们所有人能按进度工作;还有 、、和,他们和我们一起经历了整个 设计和制作过程。 4
目录 前言 ..................................................................................................................................................1 第四版的改动...........................................................................................................................2 本书结构...................................................................................................................................3 致谢...........................................................................................................................................4 第一章 快速入门...........................................................................................................................17 1.1. 编写简单的 C++ 程序..........................................................................................17 调用 GNU 或微软编译器 ....................................................................................................20 Exercises Section 1.1.1 ...........................................................................................................21 1.2. 初窥输入/输出........................................................................................................21 关键概念:已初始化变量和未初始化变量.........................................................................26 Exercises Section 1.2.2 ...........................................................................................................27 1.3. 关于注释.................................................................................................................27 Exercises Section 1.3 ......................................................................................................29 1.4. 控制结构.................................................................................................................29 关键概念:C++ 程序的缩排和格式....................................................................................31 再谈编译.................................................................................................................................34 Exercises Section 1.4.2 ...........................................................................................................35 Exercises Section 1.4.3 ...........................................................................................................37 从键盘输入文件结束符.........................................................................................................39 Exercises Section 1.4.4 ...........................................................................................................39 1.5. 类的简介.................................................................................................................39 关键概念:类定义行为.........................................................................................................42 Exercises Section 1.5.1 ...........................................................................................................43 Exercises Section 1.5.2 ...........................................................................................................45 1.6. C++ 程序 ................................................................................................................45 Exercises Section 1.6 ..............................................................................................................47 小结.................................................................................................................................47 术语.................................................................................................................................47 第一部分 基本语言.......................................................................................................................54 第二章 变量和基本类型.......................................................................................................55 2.1. 基本内置类型.........................................................................................................55 内置类型的机器级表示.........................................................................................................57 建议:使用内置算术类型.....................................................................................................60 Exercises Section 2.1.2 ...........................................................................................................61 2.2. 字面值常量.............................................................................................................61 建议:不要依赖未定义行为.................................................................................................66 Exercises Section 2.2 ..............................................................................................................67 2.3. 变量.........................................................................................................................67 Exercises Section 2.3 ..............................................................................................................68 关键概念:强静态类型.........................................................................................................69 Exercises Section 2.3.1 ...........................................................................................................70 5
术语:什么是对象?.............................................................................................................71 Exercises Section 2.3.2 ...........................................................................................................73 Exercises Section 2.3.3 ...........................................................................................................77 警告:未初始化的变量引起运行问题.................................................................................78 Exercises Section 2.3.4 ...........................................................................................................79 Exercises Section 2.3.5 ...........................................................................................................81 Exercises Section 2.3.6 ...........................................................................................................84 2.4. const 限定符 ...........................................................................................................84 Exercises Section 2.4 ..............................................................................................................87 2.5. 引用.........................................................................................................................87 术语:const 引用是指向 const 的引用...........................................................................89 Exercises Section 2.5 ..............................................................................................................90 2.6. typedef 名字............................................................................................................90 2.7. 枚举.........................................................................................................................91 2.8. 类类型.....................................................................................................................93 Exercises Section 2.8 ..............................................................................................................97 2.9. 编写自己的头文件.................................................................................................97 编译和链接多个源文件.........................................................................................................99 Exercises Section 2.9.1 .........................................................................................................102 小结...............................................................................................................................104 术语...............................................................................................................................105 第三章 标准库类型.............................................................................................................112 3.1. 命名空间的 using 声明 ........................................................................................112 Exercises Section 3.1 ............................................................................................................115 3.2. 标准库 string 类型................................................................................................115 警告:标准库 string 类型和字符串字面值.....................................................................116 Exercises Section 3.2.1 .........................................................................................................116 Exercises Section 3.2.2 .........................................................................................................119 建议:采用 C 标准库头文件的 C++ 版本.........................................................................127 Exercises Section 3.2.4 .........................................................................................................127 3.3. 标准库 vector 类型.............................................................................................127 关键概念:vector 对象动态增长.....................................................................................130 Exercises Section 3.3.1 .........................................................................................................131 关键概念:安全的泛型编程...............................................................................................134 警告:仅能对确知已存在的元素进行下标操作...............................................................135 Exercises Section 3.3.2 .........................................................................................................136 3.4. 迭代器简介...........................................................................................................136 术语:迭代器和迭代器类型...............................................................................................137 Exercises Section 3.4 ............................................................................................................142 Exercises Section 3.4.1 .........................................................................................................143 3.5. 标准库 bitset.........................................................................................................143 Exercises Section 3.5.2 .........................................................................................................150 小结...............................................................................................................................150 术语...............................................................................................................................150 6
第四章 数组和指针.............................................................................................................155 4.1. 数组.......................................................................................................................155 警告:数组的长度是固定的...............................................................................................159 Exercises Section 4.1.1 .........................................................................................................160 Exercises Section 4.1.2 .........................................................................................................162 4.2. 指针的引入...........................................................................................................162 建议:尽量避免使用指针和数组.......................................................................................163 Exercises Section 4.2.2 .........................................................................................................169 关键概念:给指针赋值或通过指针进行赋值...................................................................170 Exercises Section 4.2.3 .........................................................................................................172 Exercises Section 4.2.4 .........................................................................................................177 建议:理解复杂的 const 类型的声明.............................................................................180 Exercises Section 4.3 ............................................................................................................182 4.3. C 风格字符串 .......................................................................................................182 Exercises Section 4.3 ............................................................................................................188 C 风格字符串与 C++ 的标准库类型 string 的比较 ...................................................192 Exercises Section 4.3.1 .........................................................................................................194 Exercises Section 4.3.2 .........................................................................................................196 4.4. 多维数组...............................................................................................................196 Exercises Section 4.4.1 .........................................................................................................200 小结...............................................................................................................................200 术语...............................................................................................................................200 第五章 表达式.....................................................................................................................204 5.1. 算术操作符...........................................................................................................205 警告:溢出和其他算术异常...............................................................................................207 Exercises Section 5.1 ............................................................................................................209 5.2. 关系操作符和逻辑操作符...................................................................................209 Exercises Section 5.2 ............................................................................................................213 5.3. 位操作符...............................................................................................................213 Exercises Section 5.3.1 .........................................................................................................217 5.4. 赋值操作符...........................................................................................................218 Exercises Section 5.4.2 .........................................................................................................221 Exercises Section 5.4.3 .........................................................................................................222 5.5. 自增和自减操作符...............................................................................................222 建议:只有在必要时才使用后置操作符...........................................................................223 建议:简洁即是美...............................................................................................................224 Exercises Section 5.5 ............................................................................................................225 5.6. 箭头操作符...........................................................................................................225 Exercises Section 5.6 ............................................................................................................226 5.7. 条件操作符...........................................................................................................226 Exercises Section 5.7 ............................................................................................................228 5.8. sizeof 操作符 ........................................................................................................228 Exercises Section 5.8 ............................................................................................................229 5.9. 逗号操作符...........................................................................................................229 7
Exercises Section 5.9 ............................................................................................................230 5.10. 复合表达式的求值.............................................................................................230 Exercises Section 5.10.2 .......................................................................................................236 建议:复合表达式的处理...................................................................................................238 Exercises Section 5.10.3 .......................................................................................................239 5.11. new 和 delete 表达式........................................................................................239 警告:动态内存的管理容易出错.......................................................................................243 Exercises Section 5.11 ..........................................................................................................244 5.12. 类型转换.............................................................................................................244 Exercises Section 5.12.3 .......................................................................................................250 建议:避免使用强制类型转换...........................................................................................253 Exercises Section 5.12.7 .......................................................................................................255 小结...............................................................................................................................255 术语...............................................................................................................................256 第六章 语句.........................................................................................................................261 6.1. 简单语句...............................................................................................................261 6.2. 声明语句...............................................................................................................262 6.3. 复合语句(块)...................................................................................................263 Exercises Section 6.3 ............................................................................................................264 6.4. 语句作用域.........................................................................................................264 6.5. if 语句 ...................................................................................................................265 Exercises Section 6.5.1 .........................................................................................................270 6.6. switch 语句 ...........................................................................................................270 Exercises Section 6.6.5 .........................................................................................................278 Code for Exercises in Section 6.6.5 ......................................................................................278 6.7. while 语句.............................................................................................................279 Exercises Section 6.7 ............................................................................................................282 6.8. for 循环语句.........................................................................................................282 Exercises Section 6.8.2 .........................................................................................................286 6.9. do while 语句........................................................................................................286 Exercises Section 6.9 ............................................................................................................289 6.10. break 语句...........................................................................................................289 Exercises Section 6.10 ..........................................................................................................291 6.11. continue 语句 ......................................................................................................291 Exercises Section 6.11 ..........................................................................................................292 6.12. goto 语句.............................................................................................................292 Exercises Section 6.12 ..........................................................................................................293 6.13. try 块和异常处理 ...............................................................................................293 Exercises Section 6.13.2 .......................................................................................................297 6.14. 使用预处理器进行调试.....................................................................................299 Exercises Section 6.14 ..........................................................................................................301 小结...............................................................................................................................301 术语...............................................................................................................................302 第七章 函数.........................................................................................................................306 8
分享到:
收藏