1.vector
Vector data is arranged and operated in a very similar way to array, the only difference being the flexibility of the use of space. An array is a static space that can no longer be changed once it is configured. Vector is a dynamic space, and its internal mechanism expands itself to accommodate new elements as the elements are added.
(1) Vector iterator
Because vector maintains a continuous linear space, the normal pointer can be used as a vector iterator regardless of its element type. Because of the operational behavior performed by the vector iterator, such as operator*,operator->,operator++,operator--,operator+,operator-,operator+=, operator-=, ordinary hands are born with. As you can see, the vector provides the random Access Iterator.
The vector iterator is defined as follows:
(2) Vector data structure
the data structure used by vectors is very simple: linear space. It uses two iterators, start and finish, to point to the currently used range in the configured contiguous space, and end_of_storage the end of the entire contiguous space with an iterator :
In order to reduce the speed cost of the space configuration, the actual size of the vector may be larger than the space required by the current vector data storage for future expansion. In other words, the capacity of a vector is always greater than or equal to its size. The space usage of vectors is as follows:
Note that the so-called dynamic growth, not in the original space after the continuation of new space, but the original size of twice times a large space to configure, and then copy the original content. Therefore, any operation on the vector, once the space is reconfigured, all iterators pointing to the original vector are invalidated.
(3) Element manipulation of vectors
A.push_back (): If there is room left, use the remaining space directly. If there is no space left, open up a new, larger space, re-hold all the contents of the vector storage, and insert the new element.
B.pop_back (): Destroy the tail element, and then modulate the position of the tail end.
C.erase (first,last): Copies the last to finish element to the memory address at first start, such as:
D.insert (position,n,x): In three kinds of situations to operate: (1) The spare space is equal to the number of new elements, the number of elements after the insertion point is greater than the number of new elements. (2) reserve space is greater than or equal to the number of new elements, the number of elements after the insertion point is less than the number of new elements. (3) The spare space is less than the number of new elements. There are three cases where the following actions are appropriate:
2.list
The list is much more complex than the continuous linear space of vectors, and the advantage is that each time an element is inserted or deleted, an element space is applied or freed. Thus, the use of the list for space is absolutely accurate and not wasted at all. Also, for element insertion or element removal at any location, the list is always constant time.
(1) node of list
The node structure of the STL list is as follows: It is a two-way node structure.
(2) List of iterators
The list no longer acts as a vector with a normal pointer as an iterator because its nodes are not guaranteed to exist continuously in the storage space. The list iterator must have the ability to point to the list's node and be able to perform the correct increment, decrement , value , member access, and so on, as shown in the following:
The design of the iterator is as follows:
(3) Data structure of list
List is not only a doubly linked list, but also a circular doubly linked list, so it only requires a pointer to complete the entire list:
A list is as follows:
(4) Operation of List
The operation of the STL list is to operate on a two-way circular list. The following is an example of transfer:
"STL Source Analysis" Reading notes of the sequence container (1)