Welcome to my blog

C++ Prime 5

# 习题部分

## 第一章

### 1.14

for 循环和 while 循环会在 loop statement 前多做一次 conditional jump, do while 则不会.

## 第二章

### 2.1

C++ guarantees short and int is at least 16 bits, long at least 32 bits, long long at least 64 bits.
The signed can represent positive numbers, negative numbers and zero, while unsigned can only represent numbers no less than zero.
The C and C++ standards do not specify the representation of float, double and long double. It is possible that all three implemented as IEEE double-precision.

### 2.2

use double, or also float.

### 2.10

glo_str = “” & glo_int = 0 & loc_int 不确定 & loc_str = “”

### 2.12

C++中关于变量命名的规范：

1. 能体现变量的实际意义
2. 变量名一般采用小写字母
3. 用户自定义类名一般以大写字母开头
4. 多个单词定义的变量名应有区分Student_loan 而不是 Studentloan

### 2.13

j = 100，因为在这个语句块内，局部变量 i(100) 生效，如果想用全局变量 i(42) ，可以使用 j = ::i;

### 2.14

i = 100, sum=45, for循环语句内的i只活在该语句内，而 std::cout 在main语句块内，自然是取得100

### 2.17

10 10，等于给 i = 5 后 i = 10

42的平方

### 2.25

int 指针 ip，int 变量 i，int 引用 r 绑定 i & int 变量 i，int 指针 p 为空 & ip 指针，ip2 变量

*p = nullptr;

### 2.35

int & const int 引用 & const int 指针 & const int & 同2

c int & d int 引用

### 2.38

decltype 处理顶层 const 和引用的方式与 auto 有些许不同。

decltype((var)) 的结果永远是引用。

（包括顶层 const 和引用在内）

## 第三章

const char&

### 3.12

vector的初始化：

1：引用不可以成为vector的元素，因为其不是对象。

2：可以用花括号初始化每一个值。

3：可以用括号指定元素个数或相同的元素值

### 3.13

0 & 10个0 & 10个42 & 10 & 10,42 & 10个“” & 10个“hi”

### 3.29

1. 数组初始化必须知道大小
2. vector内置函数支持
3. vector自动扩容

hello，按字节输出

## 第四章

105

### 4.2

(vec.begin()) & (vec.begin())+1

### 4.4

((12 / 3) 4) + (5 15) +((24 % 4) / 2) = 91

### 4.5

-86 & -18 & 0 & -2

### 4.13

3.0 & 3 & 3 & 3.5

### 4.16

!=优先级高于=，(p = getPtr()) != 0 & 恒为true，if(i == 1024)

### 4.24

Since we are nesting conditional operations, we are essentially grouping expressions with each others using operators of equal precedence, i.e., two ?: operators. Here, the rules of associativity dictates how the expressions of these operators are grouped together and eventually evaluated. Right associativity means that they will be grouped from right to left as such:

Note how this grouping means that outer conditional operation will not be evaluated until the inner one is evaluated first, if the grade is less than or equal to 90.

If the operator were left associative we would have the following grouping instead:

This would result in a compile error because the outer conditional operator would have incompatible operand types: const char * and bool.

-7296

### 4.26

The C++ standard does not specify the size of integral types in bytes, but it specifies minimum ranges they must be able to hold. The minimum range of unsigned int is 0 to 65535. Since some implementations use only the minimum 16 bits for unsigned int, this could cause undefined behavior.

### 4.31

Advice: Use Postfix Operators only When Necessary

Readers from a C background might be surprised that we use the prefix increment in the programs we’ve written. The reason is simple: The prefix version avoids unnecessary work. It increments the value and returns the incremented version.The postfix operator must store the original value so that it can return the unincremented value as its result. If we don’t need the unincremented value, there’s no need for the extra work done by the postfix operator.

For ints and pointers, the compiler can optimize away this extra work. For more complicated iterator types, this extra work potentially might be more costly. By habitually using the prefix versions, we do not have to worry about whether the performance difference matters. Moreover—and perhaps more importantly—we can express the intent of our programs more directly.

## 第五章

### 5.1

To be used as spaceholder

### 5.8

if 比 else 多的时候， else 会与最近的 if 配套

## 第六章

### 6.20

If we can use const, just use it. If we make a parameter a plain reference when it could be a reference to const, the reference value maybe changed.

### 6.28

const string&

### 6.29

Depends on the type of elements of initializer_list. When the type is PODType, reference is unnecessary. Because POD is cheap to copy (such as int). Otherwise, Using reference(const) is the better choice.

*/

### 6.34

When the recursion termination condition becomes var != 0, two situations can happen :

• case 1 : If the argument is positive, recursion stops at 0.(Note : There is one extra multiplication step though as the combined expression for factorial(5) reads 5 4 3 2 1 * 1. In terms of programming languages learning, such subtle difference probably looks quite trivial. In algorithms analysis and proof, however, this extra step may be super important.)
• case 2 : if the argument is negative, recursion would never stop. As a result, a stack overflow would occur.

### 6.35

val-- 的值是 val , 陷入死循环

### 6.43

Both two should put in a header. (a) is an inline function. (b) is the declaration of useful function. we always put them in the header.

### 6.45

For example, the function arrPtr in Exercise 6.38 and make_plural in Exercise 6.42 should be defined as inline. But the function func in Exercise 6.4 shouldn’t. It is not that small and it’s only being called once. Hence, it will probably not expand as inline.

### 6.46 (Two explanations)

No. Because std::string::size() is not a constexpr function and s1.size() == s2.size() is not a constant expression.

For a non-template, non-defaulted constexpr function or a non-template, non-defaulted, non-inheriting constexpr constructor, if no argument values exist such that an invocation of the function or constructor could be an evaluated subexpression of a core constant expression (5.19), the program is ill-formed; no diagnostic required. (N3690 §7.1.5 [dcl.constexpr]/5)

constexpr 函数是指能用于常量表达式的函数，constexpr函数的返回类型和所有形参的类型都得是字面值类型，而且函数体中必须有且只有一条 return语句。

### 6.49

candidate function:

Set of functions that are considered when resolving a function call. (all the functions with the name used in the call for which a declaration is in scope at the time of the call.)

viable function:

Subset of the candidate functions that could match a given call. It have the same number of parameters as arguments to the call, and each argument type can be converted to the corresponding parameter type.

## 第七章

### 7.8

Define read‘s Sales_data parameter as plain reference since it’s intended to change the revenue‘s value.

Define print‘s Sales_data parameter as a reference to const since it isn’t intended to change any member’s value of this object.

### 7.10

we can try to divide it like that:

the condition of the if statement would read two Sales_data object at one time.

### 7.18

encapsulation is the separation of implementation from interface. It hides the implementation details of a type. (In C++, encapsulation is enforced by putting the implementation in the private part of a class)

• User code cannot inadvertently corrupt the state of an encapsulation object.
• The implementation of an encapsulated class can change over time without requiring changes in user-level code.

### 7.20

friend is a mechanism by which a class grants access to its nonpublic members. They have the same rights as members.

Pros:

• the useful functions can refer to class members in the class scope without needing to explicitly prefix them with the class name.
• you can access all the nonpublic members conveniently.
• sometimes, more readable to the users of class.

Cons:

• lessens encapsulation and therefore maintainability.
• code verbosity, declarations inside the class, outside the class.

### 7.25

The class below can rely on it. It goes in Section 7.1.5:

the synthesized versions are unlikely to work correctly for classes that allocate resources that reside outside the class objects themselves.

Moreover, the synthesized versions for copy, assignment, and destruction work correctly for classes that have vector or string members.

Hence the class below which used only built-in type and strings can rely on the default version of copy and assignment.

### 7.28

The second call to display couldn’t print # among the output, cause the call to set would change the temporary copy, not myScreen.

Yes

### 7.30

Pros

• more explicit

• can use the member function parameter which name is same as the member name.

Cons

• sometimes redundant

### 7.39

illegal. cause the call of overloaded ‘Sales_data()’ is ambiguous.

### 7.45

No problem. cause C have the default constructor.

### 7.46

• a) A class must provide at least one constructor. (untrue, “The compiler-generated constructor is known as the synthesized default constructor.”)
• b) A default constructor is a constructor with an empty parameter list. (untrue, A default constructor is a constructor that is used if no initializer is supplied.What’s more, A constructor that supplies default arguments for all its parameters also defines the default constructor)
• c) If there are no meaningful default values for a class, the class should not provide a default constructor. (untrue, the class should provide.)
• d) If a class does not define a default constructor, the compiler generates one that initializes each data member to the default value of its associated type. (untrue, only if our class does not explicitly define any constructors, the compiler will implicitly define the default constructor for us.)

### 7.47

Whether the conversion of a string to Sales_data is desired depends on how we think our users will use the conversion. In this case, it might be okay. The string in null_book probably represents a nonexistent ISBN.

Benefits:

• prevent the use of a constructor in a context that requires an implicit conversion
• we can define a constructor which is used only with the direct form of initialization

Drawbacks:

• meaningful only on constructors that can be called with a single argument

### 7.48

Both are nothing happened.

### 7.51

Such as a function like that:

if vector has not defined its single-argument constructor as explicit. we can use the function like:

What is this mean? It’s very confused.

But the std::string is different. In ordinary, we use std::string to replace const char *(the C language). so when we call a function like that:

it is very natural.

### 7.54

In C++11, constexpr member functions are implicitly const, so the “set_xx” functions, which will modify data members, cannot be declared as constexpr.

In C++14, this property no longer holds, so constexpr is suitable.

### 7.56

What is a static class member?

A class member that is associated with the class, rather than with individual objects of the class type.

What are the advantages of static members?

each object can no need to store a common data. And if the data is changed, each object can use the new value.

How do they differ from ordinary members?

• a static data member can have incomplete type.
• we can use a static member as a default argument.

## 第八章

### 8.3

putting cin in an error state cause to terminate. such as eofbit, failbit and badbit.

### 8.12

Cause we need a aggregate class here. so it should have no in-class initializers.

### 8.14

• cause they are all class type, not the built-in type. so reference more effective.
• output shouldn’t change their values. so we added the const.

## 第九章

### 9.1

list & deque & vector

### 9.3

two iterators, begin and end:

• they refer to elements of the same container.
• It is possible to reach end by repeatedly incrementing begin.

### 9.9

cbegin is a const member that returns the container’s const_iterator type.

begin is nonconst and returns the container’s iterator type.

### 9.12

The constructor that takes another container as an argument (excepting array) assumes the container type and element type of both containers are identical. It will also copy all the elements of the received container into the new one:

The constructor that takes two iterators as arguments does not require the container types to be identical. Moreover, the element types in the new and original containers can differ as long as it is possible to convert the elements we’re copying to the element type of the container we are initializing. It will also copy only the object delimited by the received iterators.

### 9.32

the statement is illegal, because as said in Standard [5.2.2] : “The order of evaluation of arguments is unspecified.” As a result, after entering function insert, iter could be its original value or original value + 1 or even anything else, depending on how compiler implemented.

### 9.35

size 是目前保存的元素数量

capacity 是在必须重新申请内存之前能存储的最大元素数量

not

### 9.37

list 不连续存储数据

array 是固定的大小

### 9.40

256 384 1024
512 768 1024
1000 1500 2000(clang is 2048)
1048 1572 2048

### 9.42

str.reserve(120)

### 9.48

string::npos

## 第十章

### 10.5

For such case, std::equal is going to compare the address value rather than the string value. So the result is not the same as std::string. Try to avoid coding this way. Check #227 for more detail.

### 10.8

Inserters like back_inserter is part of <iterator> rather than <algorithm>.

### 10.23

Assuming the function to be bound have n parameters, bind take n + 1 parameters. The additional one is for the function to be bound itself.

### 10.26

• back_inserter uses push_back.
• front_inserter uses push_front.
• insert uses insert

### 10.38

• Input iterators : ==, !=, ++, *, ->
• Output iterators : ++, *
• Forward iterators : ==, !=, ++, *, ->
• Bidirectional iterators : ==, !=, ++, --, *, ->
• Random-access iterators : ==, !=, <, <=, >, >=, ++, --, +, +=, -, -=, -(two iterators), *, ->, iter[n] == * (iter + n)

### 10.39

list have the Bidirectional iterators.

vector have the Random-access iterators.

### 10.40

• copy : first and second are Input iterators, last is Output iterators.
• reverse : Bidirectional iterators.
• unique : Forward iterators.

## 第十一章

### 11.1

map : 关联容器，元素根据键值对存储和检索，二叉搜索树实现

vector : 顺序容器，元素按其在容器中的位置顺序存储和访问，动态数组实现

### 11.2

list : 不需要随机访问数据，但希望在容器的任何位置(例如内存管理分配器中)快速插入或删除数据。

vector : 连续存储和处理一组数据时，支持随机访问和末尾的快速插入和删除。

deque : FIFO。

map : 将相关信息配对时使用映射，需要在大量数据中查找pair时。

set : 创建一个有序对象的列表(树)，以便快速遍历/搜索时。

map vs set

### 11.6

list vs set

set 单一有序，list 可重复无序

### 11.18

map<string, size_t>::const_iterator

### 11.22

map<string, int>

pair<map<string, vector<int>>, bool>

### 11.35

• use subscript operator: if a word does appear multiple times, our loops will put the last corresponding phrase into trans_map
• use insert: if a word does appear multiple times, our loops will put the first corresponding phrase into trans_map

### 11.37

• Ordered Associative Container

• Standard Traversal encounters elements in sorted order
• Order predicate may be specified
• Default order predicate is “less than”, defined using operator< for the element type
• Popular implementations: OrderedVector, BinarySearchTree
• Search operations required to have O(log n) runtime
• Insert, Remove operations should either be seldom used or have O(log n) runtime
• Unordered Associative Container

• Standard Traversal encounters elements in unspecified order
• Search, Insert, Remove operations should have average-case constant runtime
• Popular implementations use hashing

## 第十二章

### 12.4

iunsigned

### 12.5

Pros

• The compiler will not use this constructor in an automatic conversion.
• We can realize clearly which class we have used.

Cons

• We always uses the constructor to construct a temporary StrBlob object.
• cannot use the copy form of initialization with an explicit constructor. not easy to use.