C++STL series-1. Sequence container vector, list, deque

Definition of container

Containers, storage facilities.
Generally speaking, a container is a data structure.
Providing a set of sequential STL container: vector,list,deque,stack,queue,priority_queue;wherein stackand queueonly dequea makeover from, or internal data structure deque.

vector container

The data of vector is very similar to array. But the array is a static space and cannot be changed once it is configured; when it needs to be expanded, it needs to be expanded manually, which is very troublesome; while the vector is a dynamic space. With the addition of elements, the internal mechanism will automatically expand the space to accommodate new elements.
The data structure used by vector is very simple: linear continuous space . Two iterators start and finish are used to point to the used range in the configuration space, and end_of_storage points to the end of the entire continuous space.

Element operations of vector:

poc_back,erase,clear,insert等几种。

The bottom layer of the vector container is a linear continuous space, similar to an array, so it has the advantages of an array:

  1. You can quickly insert or delete elements at the end
  2. All elements in the container can be accessed directly.

However, according to the characteristics of the array, the vector container is very inefficient when inserting or deleting by position, because the position of the array is determined, and inserting by position requires moving many elements, and the time complexity is O(n);

Expansion of vector container

The expansion of the vector container is divided into the following four steps:

  1. Open up 2 times the space in the form of multiples.
  2. Copy all the old data to the new space.
  3. Free up old data space
  4. Point to the new space and resize it.
    Insert picture description here
    Although expansion and relocation are required like arrays, this step is automatic and does not require programmers to manually participate and release space. Therefore, any operation on the vector, once the space is re-allocated, all iterators pointing to the original vector will be effective.

list container

The underlying implementation of the list container is a circular doubly linked list. According to the characteristics, every time an element is inserted or deleted in the list, an element space is allocated or released. Therefore, the use of space by the list is absolutely accurate and will not be wasted. Moreover, inserting or removing elements at any position is always O(1) time complexity.
The element operation of the list: push_front,push_back,insert,erase,pop_front,pop_back,clear,reverse,sortetc. are all O(1) time complexity. However, due to the special circular doubly linked list structure, the query and access of the elements in the list container will be O(n) time complexity, because the underlying implementation is to compare one by one until the element to be accessed is found.Insert picture description here

deque container

A vector is a continuous linear space with a unidirectional opening, and a deque is a continuous linear space with a bidirectional opening. Elements can be inserted and deleted at both ends. Insert picture description here
The two biggest differences between deque and vector:

  1. deque allows direct insertion or removal of elements in the header section.
  2. Deque has no concept of capacity. You can add a space to connect at any time .

Deque roommates consist of a quantified continuous space section by section. Once it is necessary to connect a new space at the front or end of the deque. This spatial model avoids the cycle of "reconfiguration, copy, release", but it complicates deque's iterator design.

deque uses an array of map pointers, each element in it points to a larger continuous linear space, which becomes a buffer, and this buffer is the main body of the storage space. Generally speaking, the main body of this space is defaulted. It is 512 bits in size. Insert picture description here
In addition to maintaining an array of map pointers, deque also maintains start. Finish connects an iterator to point to the first buffer and the first element and the last element of the last buffer respectively.

The deque buffer expansion operation is quite complicated:
first, when push_back() is inserted at the end, when the buffer is full, push_back() will call push_back_aux() to reconfigure a new buffer and update the state of the iterator finish at the same time. Insert picture description here
After the head is full, it will first move the map down, then open up the buffer, insert from back to front, and update the state of the start iterator.Insert picture description here

Element operation and efficiency of deque

Insert picture description here
As you can see, deque is inserted at the beginning and the end. The first deletion and the last deletion are all O(1) time complexity, and container element access is also O(1) time complexity. But both erase and insert have O(n) time complexity.

to sum up

The underlying implementation of vector is similar to arrays. Its iterator is a random access iterator, and only supports quick insertion or deletion of elements at the tail, and can access any element; while the underlying implementation of list is a doubly circular linked list, and the iterator type is bidirectional Iterators, fast insertion or deletion at any position, but the efficiency of direct access is relatively low; deque is a low-level double-ended queue, and is not continuous, supports random access iterators, supports fast insertion or deletion of head or tail, random The access efficiency is also very high, but the efficiency of internal insertion or deletion is very low.

Guess you like

Origin blog.csdn.net/ALITAAAA/article/details/114480766
Recommended