关于我们
书单推荐
新书推荐
|
C++之旅(第3版) 读者对象:有其他语言编程经验,想要了解C++语言的读者;有传统C++编程经验,想要了解现代C++语言特性的读者;有较丰富编程经验且想了解C++之父在C++设计过程中的一些设计细节与思路的读者。
本书一共19章,以C++20为标准,讲述了最新的现代C++所提供的编程特性。有其他语言编程经验的读者可以从本书中快速了解C++所具备的功能,从而获得对现代C++的更全面认知,以便更好地了解现代C++语言已经发展到的程度。资深程序员可以从本书作者的整体行文风格中感受到他在设计C++特性时的一些考量及侧重点,了解C++这门语言在历史上曾经历过的变迁,以及一部分特性为什么会是今天这个样子。所以,本书适合的读者:有其他语言编程经验,想要了解C++语言的读者;有传统C++编程经验,想要了解现代C++语言特性的读者;有较丰富编程经验且想了解C++之父在C++设计过程中的一些设计细节与思路的读者。
Stroustrup 是 C++ 的设计者和原始实现者,著有《C++程序设计原理与实践》(第2版)和《C++程序设计语言》(第4版)等图书。他目前是哥伦比亚大学的教授,之前曾在 AT&T 贝尔实验室、得克萨斯 A&M 大学和摩根士丹利工作。他获得了无数荣誉,包括美国国家工程院的查尔斯·斯塔克·德雷珀奖,以表彰其开发和改进 C++ 编程语言。Stroustrup 博士是美国国家工程院院士,也是 IEEE、ACM、CHM 和剑桥丘吉尔学院的院士。
译者pansz是一个经验丰富的软件工程师,从小学开始学习编程,中学时期已成为高级程序员,迄今拥有三十年以上编程经验,对包含 C++ 在内的多种主流编程语言有着长期的深入了解,并参与制定了其供职的多家企业的编程规范。
第 1 章 基础............................................................................................................................1
1.1 引言 ...............................................................................................................................1 1.2 程序 ...............................................................................................................................2 1.2.1 Hello, World!................................................................................................................. 2 1.3 函数 ...............................................................................................................................4 1.4 类型、变量与运算........................................................................................................6 1.4.1 算术运算 ....................................................................................................................... 7 1.4.2 初始化........................................................................................................................... 9 1.5 作用域和生命周期...................................................................................................... 10 1.6 常量 ............................................................................................................................. 11 1.7 指针、数组和引用...................................................................................................... 13 1.7.1 空指针..........................................................................................................................15 1.8 检验 ............................................................................................................................. 16 1.9 映射到硬件 ................................................................................................................. 19 1.9.1 赋值..............................................................................................................................19 1.9.2 初始化..........................................................................................................................20 1.10 建议 ........................................................................................................................... 21 第 2 章 用户自定义类型........................................................................................................23 2.1 引言 ............................................................................................................................. 23 2.2 结构 ............................................................................................................................. 24 2.3 类 ................................................................................................................................. 25 2.4 枚举 ............................................................................................................................. 27 2.5 联合 ............................................................................................................................. 29 2.6 建议 ............................................................................................................................. 31 第 3 章 模块化 ......................................................................................................................32 3.1 引言 ............................................................................................................................. 32 3.2 分离编译 ..................................................................................................................... 33 3.2.1 头文件..........................................................................................................................34 3.2.2 模块..............................................................................................................................36 3.3 命名空间 ..................................................................................................................... 39 3.4 函数参数与返回值...................................................................................................... 41 3.4.1 参数传递 ......................................................................................................................42 3.4.2 返回值..........................................................................................................................43 3.4.3 返回类型推导 ..............................................................................................................45 3.4.4 返回类型后置 ..............................................................................................................45 3.4.5 结构化绑定 ..................................................................................................................46 3.5 建议 ............................................................................................................................. 47 第 4 章 错误处理...................................................................................................................48 4.1 引言 ............................................................................................................................. 48 4.2 异常 ............................................................................................................................. 49 4.3 约束条件 ..................................................................................................................... 50 4.4 错误处理的其他替代方式.......................................................................................... 52 4.5 断言 ............................................................................................................................. 54 4.5.1 assert() ..........................................................................................................................55 4.5.2 static_assert...................................................................................................................56 4.5.3 noexcept........................................................................................................................56 4.6 建议 ............................................................................................................................. 57 第 5 章 类..............................................................................................................................58 5.1 引言 ............................................................................................................................. 58 5.1.1 类的概述 ......................................................................................................................59 5.2 具体类型 ..................................................................................................................... 59 5.2.1 一种算术类型 ..............................................................................................................60 5.2.2 容器..............................................................................................................................62 5.2.3 容器的初始化 ..............................................................................................................64 5.3 抽象类型 ..................................................................................................................... 66 5.4 虚函数 ......................................................................................................................... 69 5.5 类层次结构 ................................................................................................................. 70 5.5.1 类层次结构的益处 ......................................................................................................72 5.5.2 类层次结构导航 ..........................................................................................................74 5.5.3 避免资源泄漏 ..............................................................................................................75 5.6 建议 ............................................................................................................................. 76 第 6 章 基本操作...................................................................................................................78 6.1 引言 ............................................................................................................................. 78 6.1.1 基本操作 ......................................................................................................................79 6.1.2 转换..............................................................................................................................81 6.1.3 成员初始值设定项 ......................................................................................................81 6.2 拷贝和移动 ................................................................................................................. 82 6.2.1 拷贝容器 ......................................................................................................................82 6.2.2 移动容器 ......................................................................................................................84 6.3 资源管理 ..................................................................................................................... 87 6.4 操作符重载 ................................................................................................................. 88 6.5 常规操作 ..................................................................................................................... 89 6.5.1 比较(关系操作符) ..................................................................................................90 6.5.2 容器操作 ......................................................................................................................92 6.5.3 迭代器及智能指针 ......................................................................................................92 6.5.4 输入与输出操作 ..........................................................................................................93 6.5.5 swap() ...........................................................................................................................93 6.5.6 hash<> ..........................................................................................................................93 6.6 用户自定义字面量...................................................................................................... 93 6.7 建议 ............................................................................................................................. 94 第 7 章 模板..........................................................................................................................96 7.1 引言 ............................................................................................................................. 96 7.2 参数化类型 ................................................................................................................. 97 7.2.1 受限模板参数 ..............................................................................................................99 7.2.2 模板值参数 ................................................................................................................100 7.2.3 模板参数推导 ............................................................................................................100 7.3 参数化操作 ............................................................................................................... 102 7.3.1 模板函数 ....................................................................................................................103 7.3.2 函数对象 ....................................................................................................................103 7.3.3 匿名函数表达式 ........................................................................................................105 7.4 模板机制 ................................................................................................................... 109 7.4.1 模板变量 ....................................................................................................................109 7.4.2 别名............................................................................................................................110 7.4.3 编译时 if.....................................................................................................................111 7.5 建议 ........................................................................................................................... 113 第 8 章 概念和泛型编程......................................................................................................114 8.1 引言 ........................................................................................................................... 114 8.2 概念 ........................................................................................................................... 115 8.2.1 概念的运用 ................................................................................................................116 8.2.2 基于概念的重载 ........................................................................................................117 8.2.3 有效代码 ....................................................................................................................118 8.2.4 定义概念 ....................................................................................................................119 8.2.5 概念与 auto ................................................................................................................122 8.2.6 类型与概念 ................................................................................................................124 8.3 泛型编程 ................................................................................................................... 124 8.3.1 概念的使用 ................................................................................................................124 8.3.2 使用模板实现抽象 ....................................................................................................125 8.4 可变参数模板 ........................................................................................................... 127 8.4.1 折叠表达式 ................................................................................................................128 8.4.2 完美转发参数 ............................................................................................................129 8.5 模板编译模型 ........................................................................................................... 130 8.6 建议 ........................................................................................................................... 131 第 9 章 标准库 ....................................................................................................................132 9.1 引言 ........................................................................................................................... 132 9.2 标准库组件 ............................................................................................................... 133 9.3 标准库的组织 ........................................................................................................... 134 9.3.1 命名空间 ....................................................................................................................134 9.3.2 ranges 命名空间.........................................................................................................135 9.3.3 模块............................................................................................................................136 9.3.4 头文件........................................................................................................................136 9.4 建议 ........................................................................................................................... 138 第 10 章 字符串和正则表达式.............................................................................................139 10.1 引言 ......................................................................................................................... 139 10.2 字符串 ..................................................................................................................... 139 10.2.1 string 的实现 ............................................................................................................142 10.3 字符串视图 ............................................................................................................. 142 10.4 正则表达式 ............................................................................................................. 144 10.4.1 搜索..........................................................................................................................145 10.4.2 正则表达式的符号表示...........................................................................................146 10.4.3 迭代器......................................................................................................................151 10.5 建议 ......................................................................................................................... 151 第 11 章 输入和输出 ...........................................................................................................153 11.1 引言 ......................................................................................................................... 153 11.2 输出 ......................................................................................................................... 154 11.3 输入 ......................................................................................................................... 156 11.4 I/O 状态 ................................................................................................................... 157 11.5 用户自定义类型的 I/O ........................................................................................... 158 11.6 输出格式化 ............................................................................................................. 159 11.6.1 流式格式化 ..............................................................................................................160 11.6.2 printf()风格的格式化...............................................................................................161 11.7 流 ............................................................................................................................. 164 11.7.1 标准流......................................................................................................................164 11.7.2 文件流......................................................................................................................165 11.7.3 字符串流 ..................................................................................................................165 11.7.4 内存流......................................................................................................................166 11.7.5 同步流......................................................................................................................167 11.8 C 风格的 I/O............................................................................................................ 168 11.9 文件系统 ................................................................................................................. 168 11.9.1 路径..........................................................................................................................168 11.9.2 文件和目录 ..............................................................................................................171 11.10 建议 ....................................................................................................................... 172 第 12 章 容器......................................................................................................................174 12.1 引言 ......................................................................................................................... 174 12.2 vector ....................................................................................................................... 175 12.2.1 元素..........................................................................................................................178 12.2.2 范围检查 ..................................................................................................................178 12.3 list ............................................................................................................................ 180 12.4 forward_list.............................................................................................................. 181 12.5 map .......................................................................................................................... 182 12.6 unordered_map ........................................................................................................ 183 12.7 分配器 ..................................................................................................................... 184 12.8 容器概述 ................................................................................................................. 186 12.9 建议 ......................................................................................................................... 188 第 13 章 算法......................................................................................................................190 13.1 引言 ......................................................................................................................... 190 13.2 使用迭代器 ............................................................................................................. 192 13.3 迭代器类型 ............................................................................................................. 195 13.3.1 流迭代器 ..................................................................................................................196 13.4 使用谓词 ................................................................................................................. 199 13.5 标准库算法概览 ..................................................................................................... 199 13.6 并行算法 ................................................................................................................. 201 13.7 建议 ......................................................................................................................... 202 第 14 章 范围......................................................................................................................203 14.1 引言 ......................................................................................................................... 203 14.2 视图 ......................................................................................................................... 204 14.3 生成器 ..................................................................................................................... 206 14.4 管道 ......................................................................................................................... 207 14.5 概念概述 ................................................................................................................. 208 14.5.1 类型概念 ..................................................................................................................209 14.5.2 迭代器概念 ..............................................................................................................211 14.5.3 范围概念 ..................................................................................................................212 14.6 建议 ......................................................................................................................... 213 第 15 章 指针和容器 ...........................................................................................................214 15.1 引言 ......................................................................................................................... 214 15.2 指针类型 ................................................................................................................. 215 15.2.1 unique_ptr 及 shared_ptr...........................................................................................216 15.2.2 span...........................................................................................................................219 15.3 容器 ......................................................................................................................... 221 15.3.1 array..........................................................................................................................222 15.3.2 bitset .........................................................................................................................224 15.3.3 pair............................................................................................................................225 15.3.4 tuple ..........................................................................................................................227 15.4 可变类型容器 ......................................................................................................... 229 15.4.1 variant.......................................................................................................................229 15.4.2 optional.....................................................................................................................231 15.4.3 any ............................................................................................................................232 15.5 建议 ......................................................................................................................... 233 第 16 章 实用工具...............................................................................................................234 16.1 引言 ......................................................................................................................... 234 16.2 时间 ......................................................................................................................... 235 16.2.1 时钟..........................................................................................................................235 16.2.2 日历..........................................................................................................................236 16.2.3 时区..........................................................................................................................237 16.3 函数适配 ................................................................................................................. 237 16.3.1 匿名函数作为适配器 ..............................................................................................238 16.3.2 mem_fn() ..................................................................................................................238 16.3.3 function.....................................................................................................................238 16.4 类型函数 ................................................................................................................. 239 16.4.1 类型谓词 ..................................................................................................................240 16.4.2 条件属性 ..................................................................................................................242 16.4.3 类型生成器 ..............................................................................................................242 16.4.4 关联类型 ..................................................................................................................243 16.5 source_location ........................................................................................................ 244 16.6 move()和 forward().................................................................................................. 244 16.7 位操作 ..................................................................................................................... 246 16.8 退出程序 ................................................................................................................. 247 16.9 建议 ......................................................................................................................... 248 第 17 章 数值计算...............................................................................................................249 17.1 引言 ......................................................................................................................... 249 17.2 数学函数 ................................................................................................................. 250 17.3 数值计算算法 ......................................................................................................... 251 17.3.1 并行数值算法 ..........................................................................................................251 17.4 复数 ......................................................................................................................... 252 17.5 随机数 ..................................................................................................................... 253 17.6 向量算术 ................................................................................................................. 255 17.7 数值界限 ................................................................................................................. 256 17.8 类型别名 ................................................................................................................. 256 17.9 数学常数 ................................................................................................................. 256 17.10 建议 ....................................................................................................................... 257 第 18 章 并发......................................................................................................................259 18.1 引言 ......................................................................................................................... 259 18.2 任务和 thread .......................................................................................................... 260 18.2.1 传递参数 ..................................................................................................................262 18.2.2 返回结果 ..................................................................................................................262 18.3 共享数据 ................................................................................................................. 263 18.3.1 mutex 和锁 ...............................................................................................................263 18.3.2 原子量......................................................................................................................265 18.4 等待事件 ................................................................................................................. 266 18.5 任务间通信 ............................................................................................................. 268 18.5.1 future 和 promise ......................................................................................................268 18.5.2 packaged_task...........................................................................................................269 18.5.3 async() ......................................................................................................................270 18.5.4 停止 thread ...............................................................................................................271 18.6 协程 ......................................................................................................................... 273 18.6.1 协作式多任务 ..........................................................................................................274 18.7 建议 ......................................................................................................................... 277 第 19 章 历史和兼容性........................................................................................................279 19.1 历史 ......................................................................................................................... 279 19.1.1 大事年表 ..................................................................................................................280 19.1.2 早期的 C++ ..............................................................................................................281 19.1.3 ISO C++标准............................................................................................................284 19.1.4 标准与编程风格 ......................................................................................................286 19.1.5 C++的使用...............................................................................................................287 19.1.6 C++模型...................................................................................................................287 19.2 C++特性演化 .......................................................................................................... 288 19.2.1 C++11 语言特性 ......................................................................................................288 19.2.2 C++14 语言特性 ......................................................................................................289 19.2.3 C++17 语言特性 ......................................................................................................290 19.2.4 C++20 语言特性 ......................................................................................................290 19.2.5 C++11 标准库组件 ..................................................................................................291 19.2.6 C++14 标准库组件 ..................................................................................................292 19.2.7 C++17 标准库组件 ..................................................................................................292 19.2.8 C++20 标准库组件 ..................................................................................................292 19.2.9 移除或弃用的特性 ..................................................................................................293 19.3 C/C++兼容性 .......................................................................................................... 294 19.3.1 C 与 C++是兄弟.......................................................................................................294 19.3.2 兼容性问题 ..............................................................................................................295 19.4 参考文献 ................................................................................................................. 298 19.5 建议 ......................................................................................................................... 301 附录 A std 模块...................................................................................................................303
你还可能感兴趣
我要评论
|