The C ++ STL container deque is similar to the vector and uses dynamic arrays to manage elements.
Before using deque, you must include the header file:
# Include <deque>
It is a class template defined in the namespace STD:
Template <class _ ty,
Class _ AX = Allocator <_ ty>
Class deque;
The first template parameter is used to indicate the element type. The second parameter is optional and specifies the memory model. The default memory model is generally used.
Unlike vector, deque's dynamic array is open at the beginning and end, so it can be quickly inserted and deleted at the beginning and end.
The logical structure of deque:
Deque's internal structure
Deque is an optimized basic sequence container for adding and deleting elements at both ends of a sequence. It is usually composed of several independent blocks. The first block is extended in a certain direction, and the last block is extended in another direction. It allows fast random access, but it does not store all objects in a continuous memory block like a vector, but multiple consecutive memory blocks. In addition, the tracing of these blocks and their sequence is saved in a ing structure.
Shows the internal structure:
Deque features:
1. Random Access is supported, that is, [] and at () are supported, but the performance is not good at vector.
2. You can insert or delete data internally, but the performance is not as good as list.
Difference between deque and vector:
1. Both ends can be used to quickly insert and delete elements. The vector can only be performed at the end.
2. Element access and iterator operations of deque will be slightly slower. Because deque's internal structure has an indirect process.
3. The iterator is a special smart pointer instead of a general pointer. It needs to jump between different blocks.
4. deque can contain more elements, and its max_size may be larger. Because more than one memory is used.
5. Capacity and memory allocation timing cannot be controlled.
Note: inserting or deleting an element at the beginning and end of the element will invalidate any pointers, references, and iterators pointing to the deque element. However, deque's memory reallocation is better than that of vector. Because its internal structure shows that no elements need to be copied.
6. When deque's memory block is no longer used, it will be released. The memory size of deque can be reduced. However, whether or not this is done and how it is defined by the actual version.
Similar features of deque and vector:
1. It is relatively slow to insert or delete elements in the middle part because all elements must be moved.
2. The iterator is an access iterator.
Deque is recommended:
1. You need to insert and delete elements at both ends.
2. You do not need to reference elements in the container.
3. The container is required to release elements that are no longer in use.
Operation functions of deque
Constructor and destructor:
Non-variable operations:
Variable operation:
The operations of deque are only two different points from those of vector:
Deque does not provide capacity operations: Capacity () and reverse ().
Deque directly provides a function to insert and delete the first and last elements.
The rest are the same as the vector.
Note:
1. Except for the at () function, other member functions do not check whether the index or iterator is valid.
2. Element insertion and deletion may cause memory reallocation. Therefore, any insert or delete operation will invalidate all pointers, reference, and iterators pointing to the deque element. The only exception is that after an element is inserted at the beginning and end, pointers and reference may still be valid.
Program example:
#include <iostream>#include <deque>#include <string>#include <algorithm>using namespace std;int main(){deque<string> strDeq;strDeq.assign(4,string("CHINA"));strDeq.push_back("first_string");strDeq.push_front("last_string");copy(strDeq.begin(),strDeq.end(),ostream_iterator<string>(cout," "));cout << endl;cout << endl;for(int i = 0;i < strDeq.size();++i)cout << "strDeq[" << i << "] : " << strDeq[i] << endl;cout << endl;for(int i = 0;i < strDeq.size();++i)cout << "strDeq.at(" << i << ") : " << strDeq.at(i) << endl;cout << endl;strDeq.pop_front();strDeq.pop_back();copy(strDeq.begin(),strDeq.end(),ostream_iterator<string>(cout," "));cout << endl;cout << endl;for(int i = 1;i < strDeq.size();++i)strDeq[i] = "pre" + strDeq[i];copy(strDeq.begin(),strDeq.end(),ostream_iterator<string>(cout," "));cout << endl;cout << endl;strDeq.resize(4,"resized string");copy(strDeq.begin(),strDeq.end(),ostream_iterator<string>(cout," "));cout << endl;cout << endl;}
Running result: