First, sequential container overview: A container is a collection of certain types of objects
1. Sequence container type: vector, deque, list, forward_list, array, string
- string and vector keep elements in contiguous memory space, so using subscript access is fast, but adding or removing elements at an intermediate location is time consuming
- List and forward_list are quickly added and removed from any location, but accessing an element can only traverse it, and the extra memory overhead is significant
- The purpose of forward_list design is to be comparable with the best handwritten unidirectional linked list data structures
- Deque supports subscript access, and adding or removing elements at both ends is quick, but adding or removing elements at an intermediate location is time consuming
2, unless there is good reason to choose other containers, you should use the vector
3. If you are unsure of which container to use, use only the vector and list common operations: Using iterators, not using subscript operations
4. Each container is defined in a header file with the same file name as the type name
Second, sequential containers are supported by the operation
1, in order to create a container for a copy of another container, the type of two containers and its element type must match; however, when you copy a range with an iterator, you do not need the same container type, and the element type can be converted as long as it can be
2. If the element type is a built-in type or a class type with a default constructor, you can provide only one container-size parameter for the constructor. If there is no default constructor, you need to specify the element initial value to display in addition to the size parameter
3, swap (c1, C2): Exchange two of the contents of the same type of container, it just exchanged two containers of internal data structure (name), so soon
- In addition to string, the iterator that points to the container does not invalidate the pointer and the reference after the swap. Just pointing to the content has changed
- Calling swap for string causes iterators, references, and pointers to fail
- Swap with function version is recommended
4. Assign: Sequential container use only: Replace the element with the value of the parameter in the Assign
5, the insertion of the container element is a copy
6, Push_front:list, Forward_list and deque support
7, Insert:vector, deque, list, and string are all supported, but vectors, deque, and strings can be time consuming
- Receives an insert of element number or range, and returns an iterator to the first newly added element
- The returned iterator exactly points to the new element
- Emplace constructor constructs elements directly in the container based on parameters, insert and push are copy elements to the container
8, C.back (), C.front (), c.at (n), C[n]: Access element, return is reference
- C[n] and c.at () difference is that the latter will throw a Out_of_range exception if the subscript is out of bounds
9. Erase: Returns an iterator that points to the element after the element was deleted
10. Operation supported by Forward_list
- Lst.before_begin (): iterator that returns an element that does not exist before the first element of the chain list
- Lst.insert_after (P, T): Inserts an element after the position of the iterator P
- Lst.erase_aftre (p): Delete the element after the position p points to
11. Because the code that adds or removes an element to the iterator invalidates the iterator, you must ensure that the iterator is relocated after each change to the container's operation
Three, container adapter: Stack, queue, priority_queue
1, by default, stack and queue to give deque implementation, priority_queue on the vector to achieve
Guess: Deque is just a single linked list that holds the head node, so inserting values in the middle can be slow
Nineth Chapter: Sequential containers