and
Introduction of deque
Deque online documentation description
-
deque (pronounced similar to "deck") is an acronym for the irregularity of double-ended queues. Double-ended queues are dynamically sized serial containers that can be scaled like two ends.
-
A particular library can implement deque in different ways, but it is usually a dynamic array. In any case, it allows direct access to individual elements through random access iterators, which can be dynamically scaled as needed.
-
Therefore, deque provides some functions similar to vector, but deque is more efficient in inserting and deleting data in the head and tail. Unlike vector, deque cannot guarantee that all elements are stored in a continuous space. Accessing elements by pointer and offset in deque may lead to illegal operations.
-
Vector and list provide similar interfaces, so they have similar uses, but the internal implementation principle is different: vector uses a dynamic array, the array usually needs to grow dynamically; elements in deque may be scattered in different storage blocks, Save some necessary information in the deque, usually used to directly access any element in the deque in the constant range, so the internal implementation of the deque is more complicated than the vector, but these additional information makes the deque grow more efficiently in some cases , Especially when the sequence is relatively large and the reallocation cost is relatively high.
-
In addition to frequent insertions and deletions in the head or tail, deque performs worse than list and forward_list.
Use of deque
The structure of deque
Function declaration | Interface Description |
---|---|
and () | Construct an empty double-ended queue |
deque(n, val = value_type()) | Construct a double-ended queue with n elements with value val |
deque(InputIterator first, InputIterator last) | Construct a double-ended queue with the interval of (first, last) |
and (const and x) | Copy constructor of double-ended queue |
deque iterator
The bottom of the double-ended queue is a continuous space of illusion, which is actually segmented continuous. In order to maintain its "overall continuity" illusion, it falls on the deque iterator. The following figure is the schematic diagram of deque:
Function declaration | Interface Description |
---|---|
[begin(), end()) | begin: the starting position of the container end the next position of the last element |
[rbegin (), rend ()) | The reverse iterator rbegin is at the end position and rend is at the begin |
[cbegin (), cend ()) | const iterator, the same position as begin and end, but its spatial content cannot be modified |
[crbegin (), crend ()) | const reverse iterator, with crbegin at cend position, crend at cbegin position |
deque volume operation
Function declaration | Interface Description |
---|---|
size() | Returns the number of valid elements in deque |
empty() | Check if deque is empty, return true, otherwise return false |
resize(sz, value) | Change the elements in deque to sz, the extra space is filled with value |
Deque element access operation
Function declaration | Interface Description |
---|---|
operator[] | Returns a reference to the element at position n in deque |
front() | Return a reference to the first element in the deque |
back() | Return a reference to the last element in the deque |
Modify operation in deque
Function declaration | Interface Description |
---|---|
push_back() 和 pop_back() | deque tail insertion and tail deletion |
push_front() 和 pop_front() | Deque head insertion and head deletion |
insert(pos, value)和 erase(pos) | deque insert and delete anywhere |
swap() | Swap content in two deques |
clear() | Clear the elements in deque |
Application scenarios of deque
Deque is more tasteless in serial containers, because if you simply store elements, you can use vector. If you insert or delete elements at any position, use list . So deque is rarely used. The biggest application of deque is to use it as the underlying structure of stack and queue in the standard library
Summary of main points
1. Implementation of deque:
Dynamic two-dimensional array: the
second-level pointer first allocates space to the first dimension, that is, the space of n first-level pointers, and then allocates space to each first-level pointer inside, thus forming a dynamic two-dimensional array
Deque only allocates space to the first dimension, then finds the center position pos (ensures that there is enough space forward and backward), and then allocates space to that position.
When this space is used backwards, pos is incremented by one, and then the space is applied for a new pos, and the new pos is used to continue storage. Forward is similar.
※ Forward storage will store from back to front
2. Deque iterator
The iterator has four member variables:
- Where am I in my house-current iterator position
- Where does my family start-the starting position of the one-dimensional array where the current iterator is located
- Where does my home end-the end position of the one-dimensional array where the current iterator is located
- Where is my home-the address of the one-dimensional array where the current iterator is located
3. Expansion of deque
When the space of the map is filled forward or backward, if new data needs to be stored, it must be expanded. The expansion is only for map, and does not have any impact on the existing one-dimensional array. After the map is expanded, the original pointer data will be centered into the newly applied space to complete the expansion.