Chapter 9 Sequence Container
9.1 Overview of sequential containers
In general, vector is recommended
9.2 Overview of container libraries
Operations common to all container libraries
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
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.
4. Array has a fixed size
The size of the array is also part of the type.
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
5. Assignment and swap
Assignment will cause invalidation of iterators , references, and pointers.
Usually the non-member version of swap works better.
6, relational operators
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
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.
Use the insert function to pass in the begin iterator
2. Access elements
① 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 .
③. 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
3. Delete elements
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
5. Change the size of the container
You can use resize to increase or shrink the container
6. Container operations may invalidate iterators, references, and pointers
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
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.
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
constructing string When constructing a string with a character array, it must end with a null character
2、substr
3. Other ways to change string
①. Replace is a combination method of erase and insert.
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
①, find_first_of series of functions are very useful. It is
equivalent to giving an alphabet and returning the first position of the alphabet element in the string.
4. Numerical conversion
The binary mode represented by the string and the number is different, and the conversion can be realized through a specific function.
Need to introduce the header file string
function under the std namespace
9.6 Container adapter