In STL, the basic containers are: vector, List, deque, set, map
Both set and map are unordered save elements that can be accessed only through the interface it provides.
set : A set that is used to determine whether an element is in a group, using less
Map: Map, which is equivalent to a dictionary , maps a value to another value, and if you want to create a dictionary, use it.
The bottom of the tree-based structure, most of the use of balanced binary tree implementation, find a value is a constant time, the effect is good to traverse, but each time the value is inserted, it will re-form the bottom of the balanced binary tree, the efficiency has a certain impact.
Vectors, lists, deque are ordered containers
1.Vector
Vectors are dynamic arrays. It is also allocated memory in the heap, the elements are stored continuously, there is reserved memory, and if the size is reduced, the memory will not be freed. Memory is not allocated until the new value > current size.
It has a contiguous memory space, and the starting address is the same, so it can be very well supported immediately access, that is, the [] operator, but because its memory space is continuous, so in the middle of the insert and delete will cause a copy of the memory block , in addition, when the array of memory space is not enough, You need to reapply a large enough memory and make a copy of the memory. These have greatly affected the efficiency of vectors.
The fastest operation for the last element (the fastest to add at a later time), usually does not need to move the memory, only need to keep enough memory
Adding a Delete element to the middle and start operation requires moving memory, and if your element is a struct or a class, it will be constructed and refactored at the same time, so performance is not high (it is better to put pointers to structs or classes in the vector instead of structs or classes themselves. This avoids the construction and destruction of the movement.
In terms of access, access to any element is O (1), which is constant, so vectors are often used to preserve content that needs to be randomly accessed frequently, and it is not necessary to frequently add deletions to intermediate elements.
By comparison, you can see that the vector's properties are similar to string, and you can use capacity to see the current reserved memory and swap to reduce the memory it uses.
Summary
Need regular random access please use vector
2.List
List is a doubly linked list , elements are also stored in the heap, each element is placed in a piece of memory, its memory space can be discontinuous, through the pointer to the data access, this feature makes its random access changes very inefficient , Therefore, it does not provide an overload of the [] operator. However, due to the characteristics of the list, it can be very efficient to support the deletion and insertion anywhere.
The list has no space reservation habits , so each allocation element is allocated from memory, and each element that is deleted frees up the memory it consumes.
The list where to add delete element performance is very high, do not need to move memory, of course, there is no need to construct and deconstruct each element, it is often used to do a random operation of the container.
But accessing the elements inside the list starts and ends with the fastest access
Access to other elements is O (n), so if you need frequent random access, use other good
Summarize
If you prefer to add delete large objects frequently, use list
To save a small object, the construction and destruction of the operation is not complex, you can use the vector instead of
list< pointer > is completely the lowest performing approach, in which case the vector< pointer > good is used, because the pointer is not constructed and refactored, and does not occupy much memory
3.deque
Deque is a double-ended queue (double-ended), which is also the content that is stored in the heap. It is saved in the following form:
[Heap 1]
...
[Heap 2]
...
[Heap 3]
Each heap holds several elements, and the heap and heap have pointers to each other, which looks like a combination of list and vector.
It supports the [] operator, which allows you to quickly add a delete element in front of you, or quickly add a delete element in the back, and then you can have a higher random access speed, which is similar to the efficiency of a vector, which supports operations at both ends: Push_back,push_ Front,pop_back,pop_front, and the efficiency of the list at both ends of the operation is similar.
In the standard library, the vectors and deque provide almost the same interface, the structure of which differs mainly in that the two containers are not the same in the organization memory, deque is allocated by page or block, each page contains a fixed number of elements. Instead, the vector allocates a contiguous amount of memory, Vectors are only efficient when inserting elements in the end of a sequence, and Deque's paging organization provides constant-time insert and erase operations even at the front of the container, and is more efficient than vectors in terms of volume growth
Summarize:
Vector is a quick way to add deleted elements at the end, and can quickly access any element
List is a quick way to add deleted elements to all places, but only the first and last elements can be accessed quickly
Deque is as fast as adding elements at the beginning and last, and provides a random access method, like a vector using [] to access any element, but the random access speed is not as fast as the vector, because it wants to handle the heap jump internally
Deque also has reserved space. In addition, since the deque does not require continuous space, it is important to note that the elements that can be saved are larger than the vector. There is also a high performance when adding elements in front and back without moving the elements of other blocks.
Therefore, in the actual use, how to choose which of the three containers, should be based on your needs, should generally follow the following
Principle of:
1. If you need efficient immediate access, without the efficiency of insert and delete, use vector
2, if you need a large number of insertions and deletions, and do not care about the immediate access, you should use the list
3, if you need to immediately access, and care about the end of data insertion and deletion, you should use Deque.
STL container differences: Vector list deque set map and underlying implementation