📄 c++primer
字号:
《C++ Primer, Fourth Edition》精简此精简版与原版有些出入。整理人:风子 QQ:278542295-------------------------------------------------------------------------------------------------------------------前言 Preface 第四版的改动 Changes to the Fourth Edition 本书结构 Structure of This Book 致谢 Acknowledgments第1章 快速入门 Chapter 1. Getting Started 1.1. 编写简单的 C++ 程序 Section 1.1. Writing a Simple C++ Programd 1.2. 初窥输入/输出 Section 1.2. A First Look at Input/Output 1.3. 关于注释 Section 1.3. A Word About Comments 1.4. 控制结构 Section 1.4. Control Structures 1.5. 类的简介 Section 1.5. Introducing Classes 1.6. C++ 程序 Section 1.6. The C++ Program 小结 Chapter Summary 术语 Keyterm Defined Terms第一部分: 基本语言 Part I: The Basics 第2章 变量和基本类型 Chapter 2. Variables and Basic Types 2.1. 基本内置类型 Section 2.1. Primitive Built-in Types 2.2. 字面常量 Section 2.2. Literal Constants 2.3. 变量 Section 2.3. Variables 2.4. const 限定语 Section 2.4. const Qualifier 2.5. 引用 Section 2.5. References 2.6. typedef 名字 Section 2.6. Typedef Names 2.7. 枚举 Section 2.7. Enumerations 2.8. 类类型 Section 2.8. Class Types 2.9. 编写自己的头文件 Section 2.9. Writing Our Own Header Files 小结 Chapter Summary 术语 Keyterm Defined Terms 第3章 标准库类型 Chapter 3. Library Types 3.1. 命名空间的 using 声明 Section 3.1. Namespace using Declarations 3.2. 标准库 string 类型 Section 3.2. Library string Type 3.3. 标准库 vector 类型 Section 3.3. Library vector Type 3.4. 迭代器简介 Section 3.4. Introducing Iterators 3.5. 标准库 bitset Section 3.5. Library bitset Type 小结 Chapter Summary 术语 Keyterm Defined Terms 第4章 数组和指针 Chapter 4. Arrays and Pointers 4.1. 数组 Section 4.1. Arrays 4.2. 指针的引入 Section 4.2. Introducing Pointers 4.3. C 风格字符串 Section 4.3. C-Style Character Strings 4.4. 多维数组 Section 4.4. Multidimensioned Arrays 小结 Chapter Summary 术语 Keyterm Defined Terms 第5章 表达式 Chapter 5. Expressions 5.1. 算术操作符 Section 5.1. Arithmetic Operators 5.2. 关系操作符和逻辑操作符 Section 5.2. Relational and Logical Operators 5.3. 位操作符 Section 5.3. The Bitwise Operators 5.4. 赋值操作符 Section 5.4. Assignment Operators 5.5. 自增和自减操作符 Section 5.5. Increment and Decrement Operators 5.6. 箭头操作符 Section 5.6. The Arrow Operator 5.7. 条件操作符 Section 5.7. The Conditional Operator 5.8. sizeof 操作符 Section 5.8. The sizeof Operator 5.9. 逗号操作符 Section 5.9. Comma Operator 5.10. 复合表达式的求值 Section 5.10. Evaluating Compound Expressions 5.11. new 和 delete 表达式 Section 5.11. The new and delete Expressions 5.12. 类型转换 Section 5.12. Type Conversions 小结 Chapter Summary 术语 Keyterm Defined Terms 第6章 语句 Chapter 6. Statements 6.1. 简单语句 Section 6.1. Simple Statements 6.2. 声明语句 Section 6.2. Declaration Statements 6.3. 复合语句(块) Section 6.3. Compound Statements (Blocks) 6.4. 语句作用域 Section 6.4. Statement Scope 6.5. if 语句 Section 6.5. The if Statement 6.6. switch 语句 Section 6.6. The switch Statement 6.7. while 语句 Section 6.7. The while Statement 6.8. for 循环语句 Section 6.8. The for Loop Statement 6.9. do while 语句 Section 6.9. The do while Statement 6.10. break 语句 Section 6.10. The break Statement 6.11. continue 语句 Section 6.11. The continue Statement 6.12. goto 语句 Section 6.12. The goto Statement 6.13. try 块和异常处理 Section 6.13. try Blocks and Exception Handling 6.14. 使用预处理器进行调试 Section 6.14. Using the Preprocessor for Debugging 小结 Chapter Summary 术语 Keyterm Defined Terms 第7章 函数 Chapter 7. Functions 7.1. 函数的定义 Section 7.1. Defining a Function 7.2. 参数传递 Section 7.2. Argument Passing 7.3. return 语句 Section 7.3. The return Statement 7.4. 函数声明 Section 7.4. Function Declarations 7.5. 局部对象 Section 7.5. Local Objects 7.6. 内联函数 Section 7.6. Inline Functions 7.7. 类的成员函数 Section 7.7. Class Member Functions 7.8. 重载函数 Section 7.8. Overloaded Functions 7.9. 指向函数的指针 Section 7.9. Pointers to Functions 小结 Chapter Summary 术语 Keyterm Defined Terms 第8章 标准 I/O 库 Chapter 8. The IO Library 8.1. 面向对象的标准库 Section 8.1. An Object-Oriented Library 8.2. 条件状态 Section 8.2. Condition States 8.3. 输出缓冲区的管理 Section 8.3. Managing the Output Buffer 8.4. 文件的输入和输出 Section 8.4. File Input and Output 8.5. 字符串流 Section 8.5. String Streams 小结 Chapter Summary 术语 Keyterm Defined Terms第二部分:容器和算法 Part II: Containers and Algorithms 第9章 顺序容器 Chapter 9. Sequential Containers 9.1. 顺序容器的定义 Section 9.1. Defining a Sequential Container 9.2. 迭代器和迭代器范围 Section 9.2. Iterators and Iterator Ranges 9.3. 顺序容器都操作 Section 9.3. Sequence Container Operations 9.4. vector 容器的自增长 Section 9.4. How a vector Grows 9.5. 容器的选用 Section 9.5. Deciding Which Container to Use 9.6. 再谈 string 类型 Section 9.6. strings Revisited 9.7. 容器适配器 Section 9.7. Container Adaptors 小结 Chapter Summary 术语 Keyterm Defined Terms 第10章 关联容器 Chapter 10. Associative Containers 10.1. 引言:pair 类型 Section 10.1. Preliminaries: the pair Type 10.2. 关联容器 Section 10.2. Associative Containers 10.3. map 类型 Section 10.3. The map Type 10.4. set 类型 Section 10.4. The set Type 10.5. multimap 和 multiset 类型 Section 10.5. The multimap and multiset Types 10.6. 容器的综合应用:文本查询程序 Section 10.6. Using Containers: Text-Query Program 小结 Chapter Summary 术语 Keyterm Defined Terms 第11章 泛型算法 Chapter 11. Generic Algorithms 11.1. 概述 Section 11.1. Overview 11.2. 初窥算法 Section 11.2. A First Look at the Algorithms 11.3. 再谈迭代器 Section 11.3. Revisiting Iterators 11.4. 泛型算法的结构 Section 11.4. Structure of Generic Algorithms 11.5. 容器特有的算法 Section 11.5. Container-Specific Algorithms 小结 Chapter Summary 术语 Keyterm Defined Terms第三部分:类和数据抽象 Part III: Classes and Data Abstraction 第12章 类 Chapter 12. Classes 12.1. 类的定义和声明 Section 12.1. Class Definitions and Declarations 12.2. 隐含的 this 指针 Section 12.2. The Implicit this Pointer 12.3. 类作用域 Section 12.3. Class Scope 12.4. 构造函数 Section 12.4. Constructors 12.5. 友元 Section 12.5. Friends 12.6. static 类成员 Section 12.6. static Class Members 小结 Chapter Summary 术语 Keyterm Defined Terms 第13章 复制控制 Chapter 13. Copy Control 13.1. 复制构造函数 Section 13.1. The Copy Constructor 13.2. 赋值操作符 Section 13.2. The Assignment Operator 13.3. 析构函数 Section 13.3. The Destructor 13.4. 消息处理示例 Section 13.4. A Message-Handling Example 13.5. 管理指针成员 Section 13.5. Managing Pointer Members 小结 Chapter Summary 术语 Keyterm Defined Terms 第14章 重载操作符与转换 Chapter 14. Overloaded Operations and Conversions 14.1. 重载操作符的定义 Section 14.1. Defining an Overloaded Operator 14.2. 输入和输出操作符 Section 14.2. Input and Output Operators 14.3. 算术操作符和关系操作符 Section 14.3. Arithmetic and Relational Operators 14.4. 赋值操作符 Section 14.4. Assignment Operators 14.5. 下标操作符 Section 14.5. Subscript Operator 14.6. 成员访问操作符 Section 14.6. Member Access Operators 14.7. 自增操作符和自减操作符 Section 14.7. Increment and Decrement Operators 14.8. 调用操作符和函数对象 Section 14.8. Call Operator and Function Objects 14.9. 转换与类类型 Section 14.9. Conversions and Class Types 小结 Chapter Summary 术语 Keyterm Defined Terms第四部分:面向对象编程与泛型编程 Part IV: Object-Oriented and Generic Programming 第15章 面向对象编程 Chapter 15. Object-Oriented Programming 15.1. 面向对象编程:概述 Section 15.1. OOP: An Overview 15.2. 定义基类和派生类 Section 15.2. Defining Base and Derived Classes 15.3. 转换与继承 Section 15.3. Conversions and Inheritance 15.4. 构造函数和复制控制 Section 15.4. Constructors and Copy Control 15.5. 继承情况下的类作用域 Section 15.5. Class Scope under Inheritance 15.6. 纯虚函数 Section 15.6. Pure Virtual Functions 15.7. 容器与继承 Section 15.7. Containers and Inheritance 15.8. 句柄类与继承 Section 15.8. Handle Classes and Inheritance 15.9. 再谈文本查询示例 Section 15.9. Text Queries Revisited 小结 Chapter Summary 术语 Keyterm Defined Terms 第16章 模板和泛型编程 Chapter 16. Templates and Generic Programming 16.1. 模板定义 Section 16.1. Template Definitions 16.2. 实例化 Section 16.2. Instantiation 16.3. 模板编译模型 Section 16.3. Template Compilation Models 16.4. 类模板成员 Section 16.4. Class Template Members 16.5. 一个泛型句柄类 Section 16.5. A Generic Handle Class 16.6. 模板特化 Section 16.6. Template Specializations 16.7. 重载与函数模板 Section 16.7. Overloading and Function Templates 小结 Chapter Summary 术语 Keyterm Defined Terms第五部分 高级主题 Part V: Advanced Topics 第17章 用于大型程序的工具 Chapter 17. Tools for Large Programs 17.1. 异常处理 Section 17.1. Exception Handling 17.2. 命名空间 Section 17.2. Namespaces 17.3. 多重继承与虚继承 Section 17.3. Multiple and Virtual Inheritance 小结 Chapter Summary 术语 Keyterm Defined Terms 第18章 特殊工具与技术 Chapter 18. Specialized Tools and Techniques 18.1. 优化内存分配 Section 18.1. Optimizing Memory Allocation 18.2. 运行时类型识别 Section 18.2. Run-Time Type Identification 18.3. 类成员的指针 Section 18.3. Pointer to Class Member 18.4. 嵌套类 Section 18.4. Nested Classes 18.5. 联合:节省空间的类 Section 18.5. Union: A Space-Saving Class 18.6. 局部类 Section 18.6. Local Classes 18.7. 固有的不可移植的特征 Section 18.7. Inherently Nonportable Features 小结 Chapter Summary 术语 Keyterm Defined Terms 附录 A. 标准库 Appendix A. The Library A.1. 标准库名字和头文件 Section A.1. Library Names and Headers A.2. 算法简介 Section A.2. A Brief Tour of the Algorithms A.3. 再谈 IO 库 Section A.3. The IO Library Revisited 附录B 操作符的优先级-------------------------------------------------------------------------------------------------------------------前言C++ 是一种大型的编程语言,现代 C++ 可以看成由以下三部分组成:(1) 低级语言:大部分继承 C 而来;(2) 更高级的语言特征:可以自定义数据类型,组织大规模的程序和系统;(3) 标准库:提供了一整套有用的数据结构和算法。第1章 快速入门1.1. 编写简单的 C++ 程序 C++ 的大部分基本要素:内置类型、库类型、类类型、变量、表达式、语句和函数。 // prog1.cpp int main() { return 0; } main() 函数比较特别,每个 C++ 程序必须含有 main 函数,且是唯一被操作系统显式调用的函数。 在大多数操作系统中,main() 返回一个状态值,返回值 0 通常表示成功执行完成,-1 表示失败, 非零值一般表明有错误出现。 C++ 程序文件的后缀与运行的具体编译器有关。其他的形式还包括: prog1.cxx prog1.cpp prog1.cp prog1.C GNU 编译器的默认命令是 g++: $ g++ prog1.cc -o prog1 微软编译器采用命令 cl : C:\directory> cl -GX prog1.cpp 在命令行界面(例如 UNIX 的 shell 窗口或 Windows 的命令提示窗口)编译程序。 $ CC prog1.cc 如果编译器产生一个名为 a.exe 的可执行文件,执行此程序: $ a.exe 在 UNIX 系统中,即使在当前目录,也要这样执行程序: $ ./a.exe UNIX 下查看状态: $ echo $? DOS 下查看状态: C:\> echo %ERRORLEVEL%1.2. 初窥输入/输出 C++ 的格式化输入和输出的 iostream 库 有两个类型:输入流 istream 、输出流 ostream。 流是指要从某种 I/O 设备上读入或写出的字符序列。 标准库定义了 4 个 I/O 对象:cin、cout、cerr、clog。 cin 标准输入,istream 对象,cin 读作 see-in cout 标准输出,ostream 对象,cout 读作 see-out cerr 标准错误,ostream 对象,cerr 读作 see-err clog 标准日志,ostream 对象,cerr 读作 see-log #include <iostream> // 包含 iostream 头文件,是为了使用 cin、cout 对象 int main() { std::cout << "Enter two numbers:" << std::endl; // 输出 int v1, v2; // 声明两个变量 std::cin >> v1 >> v2; // 输入 std::cout << "The sum of " << v1 << " and " << v2 << " is " << v1 + v2 << std::endl; // 输出 return 0; } endl 输出换行,并刷新输出缓冲区,用户可立即看到输出的信息。 std::cout、std::cin、std:endl 前缀 std 表示这些对象定义在命名空间 std 中的; :: 是作用域操作符。 第 3.1 节学习到程序中经常使用的避免这种冗长句法的方法。1.3. 关于注释 C++ 有两种注释:多行注释(/* ... */)、单行注释(//...) 多行注释是从 C 继承过来的,程序通常混用两种注释形式。 代码改变时,注释应与代码保持一致,错误的注释比没有注释更糟,因为它会误导后来者。 注释并不会增加可执行程序的大小,编译器会忽略所有注释。 #include <iostream> /* 简单的 main() 函数: 输入两个整数 */ int main() { // 读入(输入)两个整数 std::cout << "Enter two numbers:" << std::endl; // 输出"Enter two numbers:" int v1, v2; // 声明两个变量,未初始化 std::cin >> v1 >> v2; // 读入(输入)两个整数 return 0; }1.4. 控制结构 第6章再详细介绍各种语句。 1.4.1. while 语句 #include <iostream> int main() { int sum = 0, val = 1; // 一直循环执行到 val 大于10 为止 while (val <= 10) { sum += val; // 即 sum = sum + val ++val; // 即 val = val + 1 } std::cout << "Sum of 1 to 10 inclusive is " << sum << std::endl; return 0; } 选择哪种 C、C++ 代码风格一直在无休止的争论,没有唯一的正确风格,但保持一致性是有价值的。 在选择格式化风格时,要考虑提高程序的可读性,一旦选择了某种风格,就要始终如一地使用。 我们倾向于把函数边界的花括号自成一行,并且缩进风格,使操作符排列整齐。
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -