第1章 C 基础(C Basics) 1 1.1 **个程序(Our First Program) 1 1.2 变量(Variables) 4 1.2.1 常量(Constants) 7 1.2.2 字面量(Literals) 7 1.2.3 非窄化的初始化(non-narrowing initialization) 9 1.2.4 作用域(Scopes) 11 1.3 操作符(Operators) 13 1.3.1 算术操作符(Arithmetic Operators) 14 1.3.2 布尔操作符(Boolean Operators) 17 1.3.3 位操作符(Bitwise Operators) 18 1.3.4 赋值(Assignment) 19 1.3.5 程序流(Program Flow) 19 1.3.6 内存处理(Memory Handling) 20 1.3.7 访问操作符(Access Operators) 21 1.3.8 类型处理(Type Handling) 21 1.3.9 错误处理(Error Handling) 21 1.3.10 重载(Overloading) 22 1.3.11 操作符优先级(Operator Precedence) 22 1.3.12 避免副作用(Avoid Side Effects!) 22 1.4 表达式和语句(Expressions and Statements) 25 1.4.1 表达式(Expressions) 25 1.4.2 语句(Statements) 26 1.4.3 分支(Branching) 27 1.4.4 循环(Loops) 29 1.4.5 goto 33 1.5 函数(Functions) 33 1.5.1 参数(Arguments) 34 1.5.2 返回结果(Returning Results) 36 1.5.3 内联(Inlining) 37 1.5.4 重载(Overloading) 38 1.5.5 main函数(main Function) 40 1.6 错误处理(Error Handling) 41 1.6.1 断言(Assertions) 41 1.6.2 异常(Exceptions) 43 1.6.3 静态断言(Static Assertions) 48 1.7 I/O 48 1.7.1 标准输出(Standard Output) 48 1.7.2 标准输入(Standard Input) 49 1.7.3 文件的输入和输出(Input/Output with Files) 49 1.7.4 泛化的流概念(Generic Stream Concept) 50 1.7.5 格式化(Formatting) 51 1.7.6 处理输入输出错误(Dealing with I/O Errors) 53 1.8 数组、指针和引用(Arrays, Pointers, and References) 56 1.8.1 数组(Arrays) 56 1.8.2 指针(Pointers) 58 1.8.3 智能指针(Smart Pointers) 62 1.8.4 引用(References) 65 1.8.5 指针和引用的比较(Comparison between Pointers and References) 66 1.8.6 不要引用过期数据(Do Not Refer to Outdated Data!) 67 1.8.7 数组的容器(Containers for Arrays) 67 1.9 软件项目结构化(Structuring Software Projects) 70 1.9.1 注释(Comments) 70 1.9.2 预编译指示字(Preprocessor Directives) 71 1.10 练习(Exercises) 75 1.10.1 年龄(Age) 75 1.10.2 数组和指针(Arrays and Pointers) 76 1.10.3 读取一个矩阵市场文件的头部(Read the Header of a Matrix Market File) 76 第2章 类(Classes) 77 2.1 为普遍意义而不是技术细节编程(Program for Universal Meaning Not for Technical Details) 77 2.2 成员(Members) 79 2.2.1 成员变量(Member Variables) 80 2.2.2 可访问性(Accessibility) 80 2.2.3 访问操作符(Access Operators) 83 2.2.4 类的静态声明符(The Static Declarator for Classes) 84 2.2.5 成员函数(Member Functions) 84 2.3 设置值:构造函数和赋值(Setting Values: Constructors and Assignments) 85 2.3.1 构造函数(Constructors) 86 2.3.2 赋值(Assignment) 96 2.3.3 初始化器列表(Initializer Lists) 97 2.3.4 一致性初始化(Uniform Initialization) 99 2.3.5 移动语义(Move Semantic) 101 2.4 析构函数(Destructors) 105 2.4.1 实现准则(Implementation Rules) 105 2.4.2 适当处理资源(Dealing with Resources Properly) 106 2.5 自动生成方法清单(Method Generation Résumé) 112 2.6 成员变量访问(Accessing Member Variables) 113 2.6.1 访问函数(Access Functions) 113 2.6.2 下标操作符(Subscript Operator) 115 2.6.3 常量成员函数(Constant Member Functions) 116 2.6.4 引用限定的变量(Reference-Qualified Members) 117 2.7 操作符重载的设计(Operator Overloading Design) 118 2.7.1 保持一致!(Be Consistent!) 119 2.7.2 注意优先级(Respect the Priority) 120 2.7.3 成员函数和自由函数(Member or Free Function) 120 2.8 练习(Exercises) 123 2.8.1 多项式(Polynomial) 123 2.8.2 移动赋值(Move Assignment) 123 2.8.3 初始化器列表(Initializer List) 123 2.8.4 资源管理(Resource Rescue) 124 第3章 泛型编程(Generic Programming) 125 3.1 函数模板(Function Templates) 125 3.1.1 实例化(Instantiation) 127 3.1.2 参数类型的推导(Parameter Type Deduction) 128 3.1.3 在模板中处理错误(Dealing with Errors in Templates) 132 3.1.4 混合类型(Mixing Types) 133 3.1.5 一致性初始化(Uniform Initialization) 134 3.1.6 自动返回值类型(Automatic return Type) 134 3.2 命名空间与函数查找(Namespaces and Function Lookup) 135 3.2.1 命名空间(Namespaces) 135 3.2.2 参数相关查找(Argument-Dependent Lookup) 138 3.2.3 命名空间限定还是ADL(Namespace Qualification or ADL) 142 3.3 类模板(Class Templates) 144 3.3.1 一个容器的范例(A Container Example) 144 3.3.2 为类和函数设计统一的接口(Designing Uniform Class and Function Interfaces) 146 3.4 类型推导与定义(Type Deduction and Definition) 153 3.4.1 自动变量类型(Automatic Variable Type) 153 3.4.2 表达式的类型(Type of an Expression) 154 3.4.3 decltype(auto) 155 3.4.4 定义类型(Defining Types) 156 3.5 关于模板的一点点理论:概念(A Bit of Theory on Templates: Concepts) 158 3.6 模板特化(Template Specialization) 159 3.6.1 为单个类型特化类(Specializing a Class for One Type) 159 3.6.2 函数特化和重载(Specializing and Overloading Functions) 162 3.6.3 部分特化(Partial Specialization) 164 3.6.4 函数的部分特化(Partially Specializing Functions) 165 3.7 模板的非类型参数(Non-Type Parameters for Templates) 168 3.8 仿函数(Functors) 170 3.8.1 类似函数的参数(Function-like Parameters) 172 3.8.2 组合仿函数(Composing Functors) 173 3.8.3 递归(Recursion) 175 3.8.4 泛型归纳函数(Generic Reduction) 179 3.9 匿名函数(Lambda) 180 3.9.1 捕获(Capture) 181 3.9.2 按值捕获(Capture by Value) 181 3.9.3 按引用捕获(Capture by Reference) 182 3.9.4 广义捕获(Generalized Capture) 184 3.9.5 泛型匿名函数(Generic Lambdas) 185 3.10 变参模板(Variadic Templates) 186 3.11 练习(Exercises) 188 3.11.1 字符串表示(String Representation) 188 3.11.2 元组的字符串表示(String Representation of Tuples) 188 3.11.3 泛型栈(Generic Stack) 188 3.11.4 向量的迭代器(Iterator of a Vector) 189 3.11.5 奇数迭代器(Odd Iterator) 189 3.11.6 奇数范围(Odd Range) 189 3.11.7 bool变量的栈(Stack of bool) 190 3.11.8 自定义大小的栈(Stack with Custom Size) 190 3.11.9 非类型模板参数的推导(Deducing Non-type Template Arguments) 190 3.11.10 梯形公式(Trapezoid Rule) 190 3.11.11 仿函数(Functor) 191 3.11.12 匿名函数(Lambda) 191 3.11.13 实现make_unique(Implement make_unique) 191 第4章 库(Libraries) 192 4.1 标准模板库(Standard Template Library) 193 4.1.1 入门示例(Introductory Example) 193 4.1.2 迭代器(Iterators) 194 4.1.3 容器(Containers) 199 4.1.4 算法(Algorithms) 208 4.1.5 超越迭代器(Beyond Iterators) 215 4.2 数值(Numerics) 216 4.2.1 复数(Complex Numbers) 217 4.2.2 随机数发生器(Random Number Generators) 220 4.3 元编程(Meta-programming) 230 4.3.1 极限(Limits) 230 4.3.2 类型特征(Type Traits) 232 4.4 支持库(Utilities) 234 4.4.1 元组(Tuple) 235 4.4.2 函数(function) 238 4.4.3 引用包装器(Reference Wrapper) 240 4.5 就是现在(The Time Is Now) 242 4.6 并发(Concurrency) 244 4.7 标准之外的科学计算程序库(Scientific Libraries Beyond the Standard) 248 4.7.1 其他算术运算库(Other Arithmetics) 248 4.7.2 区间算术(Interval Arithmetic) 248 4.7.3 线性代数(Linear Algebra) 249 4.7.4 常微分方程(Ordinary Differential Equations) 249 4.7.5 偏微分方程(Partial Differential Equations) 249 4.7.6 图论算法(Graph Algorithms) 250 4.8 练习(Exercises) 250 4.8.1 按模排序(Sorting by Magnitude) 250 4.8.2 STL容器(STL Container) 250 4.8.3 复数(Complex Numbers) 250 第5章 元编程(Meta-P