"C++ Primer" Reading Notes-Chapter 9 Sequence Container

Chapter 9 Sequence Container

9.1 Overview of sequential containers

Insert picture description here
In general, vector is recommended

9.2 Overview of container libraries

Operations common to all container libraries
Insert picture description here

1, const type iterator

When write access is not required, it is best to use cbegin and cend

2. Definition and initialization of the container

Insert picture description here

3. The problem of copying between containers

There are two ways to create a new container as a copy of another container: you can directly copy the entire container; or copy a range of elements specified by an iterator.
The direct copy container needs the same type to be able to copy.
Copying through iterators only requires type compatibility.
Insert picture description here

4. Array has a fixed size

The size of the array is also part of the type.
Insert picture description here
When the array is initialized:
①, the number of initialization values
②, the array can directly copy another array. The built-in array type does not work
Insert picture description here

5. Assignment and swap

Insert picture description here
Assignment will cause invalidation of iterators , references, and pointers.
Usually the non-member version of swap works better.

6, relational operators

Insert picture description here
Only when the element type also defines the corresponding comparison operator, we can use the relational operator to compare two containers.
If the element type does not support the required operator, the container that holds this element cannot use the corresponding relational operator.

9.3 Sequential container operations

1. Add elements

Insert picture description here
Note that inserting elements will invalidate the iterators, references and pointers to the container! ! !

①. The relationship between push_back and emplace. Push_back
directly inserts a copy of the object into the container.
Emplace passes the parameters of the object constructor into the container, and then the container uses the constructor to construct the corresponding object.

②,
the first parameter of insert function insert is an iterator. In before the iterator add a new element.
The return value is an iterator of the newly added first element . You can use the return value to cyclically insert new elements.

③, you can use the insert function to realize the function of the push_front function.
Insert picture description here
Use the insert function to pass in the begin iterator

2. Access elements

Insert picture description here
① The access element returns a reference , and the modification of the reference directly affects the change of the corresponding value in the container

②, c.back returns the last element , and c.end returns an iterator to the position after the last element .
Insert picture description here
③. It is recommended to use the at function for access.
If the at member function is used for access, when the subscript is invalid, an error message will be reported.
When using the general subscript operator to access, no error will be reported when it is illegal
Insert picture description here

3. Delete elements

Insert picture description here
Pay attention to the problem of iterator failure! !

The return value of the erase function:
an iterator to the elements after the last deleted element. You can use it to delete cyclically

4. Special forward_list operation

Insert picture description here

5. Change the size of the container

You can use resize to increase or shrink the container
Insert picture description here
Insert picture description here

6. Container operations may invalidate iterators, references, and pointers

Insert picture description here
Insert picture description here
Using invalid iterators, pointers or references is a serious runtime error!
When using iterators (or pointers or references to container elements), it is a good way to minimize the program fragments that require the iterator to be valid.

① Don't save the iterator returned by end, you
Insert picture description here
need to call end() again after each insert operation

9.4 How the vector object grows

Each time the stored element exceeds the limit, the vector object will apply for a larger memory space than the newly added element in order to prepare for the subsequent addition of new elements.
Insert picture description here
Size refers to the current number of elements
. Capacity refers to the capacity of the container and the maximum number of elements that can be accommodated.

9.5 Additional string operations

The string itself is also a sequential container, and the public methods introduced earlier are also suitable for it. And here is the method that only applies to string

1. Other methods of
Insert picture description here
constructing string When constructing a string with a character array, it must end with a null character
Insert picture description here

2、substr

Insert picture description here

3. Other ways to change string

Insert picture description here
①. Replace is a combination method of erase and insert.
Insert picture description here
First call erase to delete the original, and then call insert to add the new element to the specified position

3. Search operation of string

Insert picture description here
①, find_first_of series of functions are very useful. It is
Insert picture description here
equivalent to giving an alphabet and returning the first position of the alphabet element in the string.

4. Numerical conversion

Insert picture description here
The binary mode represented by the string and the number is different, and the conversion can be realized through a specific function.

Insert picture description here
Need to introduce the header file string
function under the std namespace

9.6 Container adapter

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/youyadefeng1/article/details/114122460