[Turn] Comparison of three kinds of containers vector, list, deque

[Turn] Comparison of three kinds of containers vector, list, deque

Original blog address

Vector

It is a ** linear sequence structure. **It is equivalent to an array, but its size does not need to be specified in advance and it is automatically expanded. It can be operated like an array, and because of its characteristics, we can completely regard vector as a dynamic array.
After creating a vector, it will automatically allocate a contiguous memory space in memory for data storage. The initial space size can be specified in advance or by default by vector. This size is the return value of the capacity() function. When the stored data exceeds the allocated space , vector will reallocate a block of memory, but such allocation is very time-consuming. It will do this when reallocating space:

First, vector will apply for a larger memory block;

Then, copy the original data to the new memory block;

Second, destroy the object in the original memory block (call the destructor of the object);

Finally, release the original memory space.

If the amount of data stored by the vector is large, such operations will definitely lead to poor performance (this is also the reason why the vector is designed to be a value type that is easier to copy). So the performance of vector is not good under all circumstances, the performance of vector is optimal only when its size is known in advance.

Features of vector:

  1. Specify a piece of continuous storage like an array, but the space can be dynamically expanded. That is, it can be operated like an array and can be operated dynamically. Usually reflected in push_back() pop_back().
  2. Random access is convenient, it is accessed like an array, that is, it supports the [] operator and vector.at()
  3. Save space, because it is stored continuously, and the area where the data is stored is not wasted, but it must be clear that the vector is not fully stored in most cases, and it is actually wasted in the area that is not stored.
  4. The efficiency of inserting and deleting internally is very low, and such operations are basically prohibited. Vector is designed to only add and delete operations at the back end. The reason is that the internal implementation of vector is based on the principle of sequence table.
  5. Push and pop can only be performed at the end of the vector, not at the head of the vector.
  6. When the dynamically added data exceeds the default allocated size of the vector, the memory must be reallocated, copied, and released. This operation is very performance-intensive. Therefore, to achieve the optimal performance of the vector, it is best to specify its space when creating the vector.

Doubly linked list

Is a linear linked list structure , its data is composed of several nodes, each node includes an information block (that is, the actual stored data), a predecessor pointer and a trailing pointer. It does not need to allocate a specified memory size and can scale arbitrarily, because it is stored in a non-contiguous memory space, and the ordered elements are linked by pointers.

Due to its structure, the performance of list random retrieval is very bad, because it does not directly find the address of the element like a vector , but searches in order from the beginning one by one, so that the later the target element, its retrieval time The longer. The retrieval time is proportional to the position of the target element.

Although the speed of random retrieval is not fast enough, it can quickly insert and delete operations at any node. Because each node of the list saves its position in the linked list, inserting or deleting an element only affects up to three elements, unlike vector, which affects the storage addresses of all elements after the operation point, this One point is that vector is incomparable.

Features of list:

  1. Do not use continuous memory space so that dynamic operations can be carried out at will;
  2. You can quickly insert or delete at any position inside, of course, you can also push and pop at both ends.
  3. Internal random access is not allowed, that is, the [] operator and vector.at() are not supported;
  4. Compared with verctor, it takes up more memory.

Deque

It is an optimized basic sequence container for adding and deleting elements at both ends of the sequence. It allows relatively fast random access, but it does not store all objects in a contiguous memory block like a vector, but uses multiple contiguous memory blocks, and keeps track of these blocks and their order in a mapping structure . The overhead of adding or removing elements to both ends of the deque is minimal. It does not need to reallocate space, so adding elements to the end is more efficient than vector.

In fact, deque is a combination of the advantages and disadvantages of vector and list. It is a kind of container in between.

Features of deque:

  1. Random access is convenient, that is, it supports the [] operator and vector.at(), but the performance is not as good as vector;
  2. Can insert and delete operations internally, but the performance is not as good as list;
  3. You can push and pop at both ends;

Comparison of the three

  • A vector is a contiguous memory block , and a deque is a plurality of contiguous memory blocks. The list is all data elements stored separately. It can be that any two elements are not continuous.

  • The query performance of vector is the best, and it is also good to add data at the end, unless it re-applies the memory segment; suitable for efficient random storage.

  • List is a linked list, any element can be discontinuous, but it has two pointers to the previous element and the next element. Therefore, it has the best performance for inserting and deleting elements, but the query performance is very poor; it is suitable for a large number of insert and delete operations without caring about random access requirements.

  • Deque is somewhere in between. It takes into account the advantages of arrays and linked lists. It is a combination of block linked lists and multiple arrays. So it has good query performance by list, and good insertion and deletion performance by vector. If you need to access immediately and care about the insertion and deletion of data at both ends , then deque is the best choice.

Guess you like

Origin blog.csdn.net/weixin_44338712/article/details/108137287