Notes on "C++: A Beginner's Guide (10th Edition)"

写在学《C++入门经典(第10版)》的时候,主要是记录一些以前没有注意,不知道的地方。

强力安利这本书,讲的浅显易懂,而且使用的技术都比较新,非常适合自学的人。

全文都是我一个字一个字打上来的,为了省事,会适量忽略一些不重要信息(比如要包含声明头文件,反正vs有提示,就不打了)。

第2章 C++基础知识

转义序列

名称 转义序列
换行符 \n
水平制表符 \t
响铃符 \a
反斜杠 \
双引号 "

此外, C++11 支持原始字符串字面值,适合于有大量字符需要转义的情况。
要求以R开头,而且字符串内容要放到圆括号内,圆括号外还要再加一对双引号。
以下代码输出字符串字面值 “c:\Windows"。

1
cout << R"(c:\Windows\)";

格式化带小数点的数字

传送门

第3章 更多的控制流程

枚举类型(选读)

枚举类型是值用一组 int 类型的常量来定义的类型。枚举类型就像是包含了一组声明常量的列表。
枚举类型中,两个或更多的命名常量可接受同一个 int 值。
如果不指定任何数值,枚举类型中的标识符会被自动分配一组连续的值,这些值的第一个是0,其余常量是上一个值增加1——除非显式指定了一个或多个枚举常量的值。

1
2
3
enum Direction { NORTH = 0, SOUTH = 1, EAST = 2, WEST = 3 }
enum Direction { NORTH, SOUTH, EAST, WEST }
//以上两条语句等价。

C++11 中引入了一种新的枚举,称为强枚举枚举类
为定义强枚举,在 enum 后添加关键字 class 就可以了。

再论递增操作符和递减操作符

++在变量之前,先递增再返回值;++在变量之后,先返回值再递增。

第4章 过程抽象和返回值的函数

一些预定义函数

名称 说明
sqrt 平方根
pow 乘方(幂)(实参,返回值均为 double )
abs 取 int 的绝对值
labs 取 long 的绝对值
fabs 取 double 的绝对值
ceil 向上取整
floor 向下取整
srand 为随机数生成器提供种子值
rand 随机数

强制类型转换

1
2
3
4
5
6
7
8
9
static_cast<目标类型>(表达式);
// int 转 double
static_cast<double>(3);

//强制类型转换的古老形式
类型名(表达式);
double(3);

//虽然说要尽量使用第一种,但是第二种打的字少一些,所以...

另一种形式的函数声明

函数声明中不一定要列出形参名称,以下两个函数声明等价:

1
2
double abc(int a, double b);
double abc(int, double);

但应坚持使用第一种形式,以确保可读性。

第5章 用函数完成所有子任务

传引用参数

有时候有奇效,用起来超级舒服。

1
2
3
4
// 普通的(传值调用函数)声明方法
int abc(int a);
// 传引用函数声明方法
int abc(int& a);

第6章 I/O流——对象和类入门

文件I/O

1
2
3
4
5
6
7
8
9
using namespace std;
ifstream inStream;
ofstream outStream;

inStream.open("infile.dat");
outStream.open("outfile.dat");

inStream.close();
outStream.close();

追加到文件(选读)

1
2
ofstream fout;
fout.open("data.txt",ios::app);

用流函数格式化输出

每个输出流的成员函数(部分):

标志 含义
precision() 指定小数点后不同位数
setf() 设置标志
width() 输出项占多少个空格(域宽)(用于左右对齐)

setf()的格式化标志:

标志 含义
ios::fixed 不用科学计数法
ios::scientific 用科学计数法
ios::showpoint 显示浮点数后面所有0
ios::showpos 正整数之前会输出正号
ios::right 右对齐
ios::left 左对齐

任何标志都可以使用unsetf()取消。

操纵元

标志 含义
endl 这个不说了
setw 和width完全一样
setprecision 和precision完全一样

这玩意感觉挺多余的。。

字符串I/O

put(),get(),putback()

万用型流参数

适用于有时候是cin,有时候是输入文件流的情况。

名称 关键字
输入流 istream
输出流 ostream

函数的默认实参(选读)

1
void newLine(istream& inStream = cin);

检查文件结尾

方法1:

1
2
3
4
5
6
7
double next, sum = 0;
int count = 0;
while (inStream >> next)
{
sum += next;
count++;
}

方法2:
eof成员函数。

预定义字符函数

第7章 数组

C++11基于范围的for语句

C++11中一种新的for循环,用于快速遍历数组元素。

1
2
3
int arr[]={1,2,3,4,5};
for(auto i : arr)
cout<<i;

甚至可以这样

1
2
3
4
5
int arr[]={1,2,3,4,5};
for(int& i : arr)
i++;
for(int i : arr)
cout<<i;

整个数组作为函数参数

和传引用参数用法差不多。
为了防止不经意间的修改,一般使用const修饰。

第8章 字符串和向量

预定义C字符串函数

函数 说明 提示
strcpy(Target_string,Src_string) 将Src_string中的值复制到Target_string中 不检查Target_string中最大可存储的容量
strcat(Target_string,Src_string) 将Src_string中的值连接到Target_string末尾 不检查Target_string中最大可存储的容量
strlen(Src_string) 返回Src_string长度的整数(空字符’\0’不计算在内)
strcmp(String_1, String_2) 如果两个字符串相等,就返回0 如果相等,会返回0,它会转换成false。注意这可能与你想象的相反。

如果需要检查Target_string中最大可存储的容量,可以使用例如strcpy_s,后面带_s的函数。

标准string类

getline成员函数

有两个版本

1
2
istream& getline(istream& ins, string& strVar, char delimiter);
istream& getline(istream& ins, string& strVar);

第一个版本允许你自定义定界符,第二个版本默认定界符为’\n’

注意,混用cin和getline的时候需要注意,cin会把’\n’给getline,导致getline读到空字符串。

成员函数

示例 说明
元素访问
str[i]
str.at(i)

str.substr(position, length)
str,length()

返回对索引i处的引用,不检查非法索引
和上面一样,但是这个版本会检查非法索引
返回调用对象的一个子字符串,从position开始,含有length个字符
返回长度
赋值/修改
str.empty()
str.insert(pos, str2)
str.erase(pos, length)

如果str为空字符串,就返回true,否则返回false
在str的pos处插入str2
删除长度为length的子字符串,从pos处开始
查找
str.find(str1)

str.find(str1, pos)
str.find_first_of(str1, pos)
str.find_first_not_of(str1, pos)

返回str1在str中首次出现的索引。如果str1没找到,返回特殊值string::npos
返回str1在str中首次出现的索引,从pos出开始查找
返回str1的任何字符在str中首次出现的索引,从pos处开始查找
返回不属于str1的任何字符在str中首次出现的索引,从pos处开始查找

小细节

赋值操作不适应于C字符串,所以下面这个看似合法的语句其实是非法的。

1
aCString = stringVariable.c_str();  //非法

字符串转换

C++11下的函数(C++11前滚)
转化到数字
stof(不会吧还有人用float),stod,stoi,stol
各种奇怪的类型转换为字符串
to_string

向量

最喜欢的功能之一,必须拥有名字,安排上。
可以自动扩充还一下子就知道长度的高级数组谁不喜欢呢

第9章 指针和动态数组

基本内存管理

new完了记得要delete。
另外delete之后的虚悬指针记得要指向nullptr。

Written while studying “C++: A Beginner’s Guide (10th Edition)”, mainly to record things I didn’t notice or didn’t know before.

Highly recommend this book; it’s explained in simple terms and uses relatively new technologies, making it very suitable for self-learners.

The entire content was typed out character by character by me. To save effort, some unimportant information will be appropriately omitted (e.g., including header file declarations; since VS has IntelliSense, they won’t be typed).

Chapter 2: C++ Basics

Escape Sequences

Name Escape Sequence
Newline \n
Horizontal Tab \t
Bell (Alert) \a
Backslash \
Double Quote "

Additionally, C++11 supports raw string literals, which are suitable for situations requiring many escape characters.
It must start with R, and the string content must be placed within parentheses, with an extra pair of double quotes outside the parentheses.
The following code outputs the string literal “c:\Windows".

1
cout << R"(c:\Windows\)";

Formatting Numbers with Decimal Points

Link

Chapter 3: More Control Flow

Enumeration Types (Optional Reading)

An enumeration type is a type whose values are defined by a set of int constants. An enumeration type is like a list containing a set of declared constants.
Within an enumeration type, two or more named constants can accept the same int value.
If no values are specified, the identifiers in the enumeration type are automatically assigned a sequence of values, the first of which is 0, and each subsequent constant is one greater than the previous—unless one or more enumeration constant values are explicitly specified.

1
2
3
enum Direction { NORTH = 0, SOUTH = 1, EAST = 2, WEST = 3 }
enum Direction { NORTH, SOUTH, EAST, WEST }
//The above two statements are equivalent.

C++11 introduced a new type of enumeration called strongly-typed enums or enum classes.
To define a strong enum, simply add the keyword class after enum.

More on the Increment (++) and Decrement (–) Operators

If ++ is before a variable, it increments first, then returns the value; if ++ is after a variable, it returns the value first, then increments.

Chapter 4: Procedural Abstraction and Functions That Return a Value

Some Predefined Functions

Name Description
sqrt Square root
pow Power (exponentiation; parameters and return value are double)
abs Absolute value of int
labs Absolute value of long
fabs Absolute value of double
ceil Round up
floor Round down
srand Provide a seed value for the random number generator
rand Random number

Type Casting

1
2
3
4
5
6
7
8
9
static_cast<target_type>(expression);
// int to double
static_cast<double>(3);

//Old-fashioned form of type casting
type_name(expression);
double(3);

//Although the first form is recommended, the second one is shorter to type, so...

Another Form of Function Declaration

Function declarations do not necessarily need to list parameter names; the following two function declarations are equivalent:

1
2
double abc(int a, double b);
double abc(int, double);

However, the first form should be adhered to for readability.

Chapter 5: Using Functions for All Sub-Tasks

Reference Parameters

Sometimes surprisingly effective and very comfortable to use.

1
2
3
4
// Standard (call-by-value) function declaration method
int abc(int a);
// Reference parameter function declaration method
int abc(int& a);

Chapter 6: I/O Streams—An Introduction to Objects and Classes

File I/O

1
2
3
4
5
6
7
8
9
using namespace std;
ifstream inStream;
ofstream outStream;

inStream.open("infile.dat");
outStream.open("outfile.dat");

inStream.close();
outStream.close();

Appending to a File (Optional Reading)

1
2
ofstream fout;
fout.open("data.txt",ios::app);

Formatting Output with Stream Functions

Member functions (partially) for each output stream:

Flag Meaning
precision() Specify the number of digits after the decimal point
setf() Set flags
width() Number of spaces an output item occupies (field width) (for left/right alignment)

Formatting flags for setf():

Flag Meaning
ios::fixed Do not use scientific notation
ios::scientific Use scientific notation
ios::showpoint Display all trailing zeros for floating-point numbers
ios::showpos Output a plus sign before positive numbers
ios::right Right align
ios::left Left align

Any flag can be canceled using unsetf().

Manipulators

Flag Meaning
endl This one is self-explanatory
setw Exactly the same as width
setprecision Exactly the same as precision

These feel a bit redundant…

String I/O

put(), get(), putback()

Universal Stream Parameters

Applicable for situations where sometimes it is cin, sometimes an input file stream.

Name Keyword
Input stream istream
Output stream ostream

Default Function Arguments (Optional Reading)

1
void newLine(istream& inStream = cin);

Checking for End of File

Method 1:

1
2
3
4
5
6
7
double next, sum = 0;
int count = 0;
while (inStream >> next)
{
sum += next;
count++;
}

Method 2:
The eof member function.

Predefined Character Functions

Omitted.

Chapter 7: Arrays

C++11 Range-based for Statement

A new type of for loop in C++11 for quickly traversing array elements.

1
2
3
int arr[]={1,2,3,4,5};
for(auto i : arr)
cout<<i;

You can even do this:

1
2
3
4
5
int arr[]={1,2,3,4,5};
for(int& i : arr)
i++;
for(int i : arr)
cout<<i;

Entire Array as a Function Argument

Usage is similar to reference parameters.
To prevent inadvertent modifications, const is generally used for decoration.

Chapter 8: Strings and Vectors

Predefined C-String Functions

Function Description Note
strcpy(Target_string,Src_string) Copies the value from Src_string into Target_string Does not check the maximum capacity of Target_string
strcat(Target_string,Src_string) Concatenates the value of Src_string to the end of Target_string Does not check the maximum capacity of Target_string
strlen(Src_string) Returns an integer representing the length of Src_string (null character ‘\0’ not counted)
strcmp(String_1, String_2) Returns 0 if the two strings are equal If equal, it returns 0, which converts to false. Note this may be the opposite of what you’d expect.

To check the maximum capacity of Target_string, you can use functions like strcpy_s, functions with _s suffix.

Standard string Class

getline Member Function

Two versions:

1
2
istream& getline(istream& ins, string& strVar, char delimiter);
istream& getline(istream& ins, string& strVar);

The first version allows you to define a custom delimiter; the second version defaults the delimiter to ‘\n’.

Note: When mixing cin and getline, be careful because cin leaves ‘\n’ for getline, causing getline to read an empty string.

Member Functions

Example Description
Element Access
str[i]
str.at(i)

str.substr(position, length)
str.length()

Returns a reference to the element at index i; does not check for illegal indices.
Same as above, but this version checks for illegal indices.
Returns a substring of the calling object, starting at position and containing length characters.
Returns the length.
Assignment/Modification
str.empty()
str.insert(pos, str2)
str.erase(pos, length)

Returns true if str is an empty string; otherwise, returns false.
Inserts str2 at position pos in str.
Deletes a substring of length length, starting at position pos.
Searching
str.find(str1)

str.find(str1, pos)
str.find_first_of(str1, pos)
str.find_first_not_of(str1, pos)

Returns the index of the first occurrence of str1 in str. If str1 is not found, returns the special value string::npos.
Returns the index of the first occurrence of str1 in str, starting from position pos.
Returns the index of the first occurrence of any character from str1 in str, starting from position pos.
Returns the index of the first occurrence of any character not in str1 in str, starting from position pos.

Small Details

Assignment operations do not apply to C-strings, so the following seemingly valid statement is actually illegal.

1
aCString = stringVariable.c_str();  //Illegal

String Conversions

C++11 functions (pre-C++11 is outdated)
Converting to numbers:
stof (who still uses float?), stod, stoi, stol
Converting various odd types to strings:
to_string

Vectors

One of my favorite features; it must be given a name, put it on the list!
Who wouldn’t like an advanced array that automatically expands and immediately knows its length?

Chapter 9: Pointers and Dynamic Arrays

Basic Memory Management

Remember to delete after using new.
Also, remember to set dangling pointers to nullptr after delete.


Notes on "C++: A Beginner's Guide (10th Edition)"
https://tokisaki.top/blog/c-study-notes/
作者
Tokisaki Galaxy
发布于
2021年3月13日
许可协议