vector list deque set map-underlying implementation

The difference between stl containers: vector list deque set map-underlying implementation

The difference between stl containers: vector list deque set map (turn)

The basic containers in STL are: vector, list, deque, set, map

Both set and map are unordered storage elements, and the elements inside can only be accessed through the interface they provide

SET : collection, used to determine one element in a group which is not used for less
Map : mapping, corresponding to the dictionary , is mapped to a value to another value, if you want to create the dictionary, then use it to good
underlying employed It is a tree structure, most of which are implemented using a balanced binary tree. Finding a certain value is a constant time, and the effect is good when traversing it, but every time a value is inserted, the underlying balanced binary tree will be reconstructed, which has a certain effect on efficiency.

Vector, list, and deque are ordered containers
1. Vector
vector is a dynamic array. It also allocates memory in the heap, with elements stored continuously, and reserved memory. If the size is reduced, the memory will not be released. If the new value> the current large Memory will be allocated only after hours.

It has a continuous memory space, and the starting address remains unchanged, so it can very well support random access, that is, the [] operator, but because its memory space is continuous, inserting and deleting in the middle will This causes a copy of the memory block . In addition, when the memory space behind the array is not enough, it is necessary to reapply for a large enough memory and copy the memory. These have greatly affected the efficiency of vector.

The operation of the last element is the fastest (adding and deleting is the fastest), at this time generally there is no need to move the memory, only when the reserved memory is not enough

 

Adding and deleting elements in the middle and beginning requires moving the memory. If your element is a structure or a class, then construction and destruction operations will be performed while moving, so the performance is not high (it is better to use the pointer of the structure or class Put it into the vector instead of the structure or the class itself, so that it can avoid the construction and destruction when moving).
In terms of access, access to any element is O(1), which is constant, so vector is often used to store content that requires frequent random access, and does not need to frequently add and delete intermediate elements.

In comparison, you can see that the attributes of vector are similar to those of string.You can also use capacity to see the currently reserved memory, and use swap to reduce the memory it uses.

capacity() returns the number of elements that the vector can hold (without reallocating memory) test push_back 1000 data capacity returns 16384

 

Summary If you
need frequent random access, please use vector

2. The list
list is a doubly linked list . The elements are also stored in the heap. Each element is placed in a piece of memory. Its memory space can be discontinuous. The data is accessed through pointers. This feature makes it random Access becomes very inefficient , so it does not provide overloading of the [] operator. However, due to the characteristics of the linked list, it can efficiently support deletion and insertion at any place.

The list has no space reservation habit , so every time an element is allocated, it will be allocated from memory, and every time an element is deleted, the memory it occupied will be released.

The performance of adding and deleting elements in the list is very high. There is no need to move the memory. Of course, there is no need to construct and destruct each element, so it is often used to do random operation containers.
But when you access the elements in the list, you start and The last access is the fastest.
Access to other elements is O(n), so if you need frequent random access, it is better to use other

Summary
If you like to add and delete large objects frequently, then please use the list
to save the objects that are not large, and the construction and destruction operations are not complicated. Then you can use vector instead of
list<pointer>, which is completely the lowest performance approach. In this case or the use of vector <pointer> good, because the pointer is not configured destructor does not take up a lot of memory

3. the deque
the deque is a deque (double-ended queue), also in the content is stored in the stack which is stored in the form of As follows:
[Heap 1]
...
[Heap 2]
...
[Heap 3]
Each heap stores several elements, and then there is a pointer between the heap and the heap, which looks like a combination of list and vector.

It supports the [] operator, that is, it supports random access, allowing you to quickly add and delete elements in the front, or quickly add and delete elements in the back, and then it can also have a relatively high random access speed and vector efficiency There is little difference, it supports operations at both ends: push_back, push_front, pop_back, pop_front, etc., and the efficiency of the operation at both ends is similar to that of the list.
In the standard library, vector and deque provide almost the same interface. The difference in structure is mainly that the two containers are different in organizing memory. Deque allocates memory by page or block, and each page contains a fixed number of elements. On the contrary, vector allocates a continuous memory, vector is only efficient when inserting elements at the end of the sequence, and the paging organization of deque can provide constant-time insert and erase operations even at the front end of the container, and in terms of volume growth Also more efficient than vector

Summary:
Vector can quickly add and delete elements at the end, and can quickly access any element.
List can quickly add and delete elements in all places, but can only quickly access the first and last elements.
Deque is added at the beginning and the end. The elements are all the same fast and provide a random access method. Use [] to access any element like a vector, but the random access speed is not as fast as a vector, because it needs to internally handle the heap jump and
deque also has reserved space. In addition, because deque does not Requires continuous space, so the elements that can be saved are larger than vectors. This point should also be noted. Also, there is no need to move other blocks of elements when adding elements in the front and back, so the performance is also very high.

Therefore, in actual use, how to choose which of these three containers should be determined according to your needs. Generally, the following
principles should be followed :
1. If you need efficient random access, and do not care about the efficiency of insertion and deletion, Use vector
2. If you need a large number of insertions and deletions, but don't care about random access, you should use list
3. If you need to access random access and care about the insertion and deletion of data at both ends, you should use deque.

Guess you like

Origin blog.csdn.net/csdn1126274345/article/details/102963770