Introduction to Deque_Dequeis "double-ended"
The abbreviation for queue, like vectors, is a container of STL, deque is a double-ended array, and vectors are single-ended.
DequeQue is very similar to vector s in interface and can be replaced directly in many operations. _Dequecan access elements randomly (supports direct access to index values,
Use the [] operator or at() method, which will be described in more detail later. The deque head and tail add or remove elements very quickly. But placing or removing elements in the middle can be time consuming.
The default construction of the #include deque Object deques is implemented using a template class, and the default construction of the dequeObject is deque
deqT; deque deqInt; // A dequeContainer for int s. deque
deq Float; // A dequecontainer for floats. deque deq String;
//A deque container for string s....// You can also set pointer types or custom types within angle brackets.
Addition and removal operation theory at the end of dequeQue: deque.push_back(elem); // Add a data at the end of the container
deque.push_front(elem); // Insert a data deque at the head of the container. Pop_ Back();
//Delete container last data deque.pop_front(); // Delete Container First Data
deque deqInt; deqInt.push_back(1); deqInt.push_back(3);
deqInt.push_back(5); deqInt.push_back(7); deqInt.push_back(9);
deqInt.pop_front(); deqInt.pop_front(); deqInt.push_front(11);
deqInt.push_front(13); deqInt.pop_back(); deqInt.pop_back();
//deqInt { 13,11,5}
Deque's theoretical knowledge of data access:deque.at(idx);
//Returns the data indicated by the index idx, throws out_if the IDX is out of bounds Of_ Range. deque[idx];
//Returns the data referred to by the index idx, and if the IDX goes beyond the bounds, no exceptions are thrown and a direct error occurs. deque.front(); // Returns the first data.
deque.back(); // Returns the last data deque deqInt; deqInt.push_back(1);
deqInt.push_back(3); deqInt.push_back(5); deqInt.push_back(7);
deqInt.push_back(9);
int iA = deqInt.at(0); //1 int iB = deqInt[1]; //3 deqInt.at(0) = 99; //99 deqInt[1] = 88; //88 int iFront = deqInt.front(); //99 int iBack = deqInt.back(); //9 deqInt.front() = 77; //77 deqInt.back() = 66; // 66 deque s and iterators
Theoretical knowledge Begin(); // Returns the iterator for the first element in the container. deque.end();
//Returns the iterator after the last element in the container. deque.rbegin(); // Returns the iterator of the first to last element in the container.
deque.rend(); // Returns the iterator after the last last element in the container.
deque deqInt; deqInt.push_back(1); deqInt.push_back(3);
deqInt.push_back(5); deqInt.push_back(7); deqInt.push_back(9);
for (deque<int>::iterator it=deqInt.begin(); it!=deqInt.end(); ++it) { cout << *it; cout << ""; } // 1 3 5 7 9 for (deque<int>::reverse_iterator rit=deqInt.rbegin();
rit!=deqInt.rend(); ++rit) { cout << *rit; cout << ""; }
//9 7 5 3 1 deque object with parameter construction theory knowledge_deque(beg,end);// The constructor will [beg,
end) The elements in the interval are copied to themselves. Note that this area is left closed and right open. deque(n,elem); // The constructor copies n elems to itself.
_deque(const deque &deq);// Copy constructor.
deque deqIntA; deqIntA.push_back(1); deqIntA.push_back(3);
deqIntA.push_back(5); deqIntA.push_back(7);
deqIntA.push_back(9);
deque<int> deqIntB(deqIntA.begin(),deqIntA.end()); //1 3 5 7 9 deque<int> deqIntC(5,8); //8 8 8 8 8 deque<int>
DeqIntD (deqIntA); // 1 357 9 deque's assignment theory knowledge
deque.assign(beg,end); // Assign a copy of the data in the [beg, end] interval to itself. Note that the interval is left closed and right open.
deque.assign(n,elem); // Assign n elem copies to itself. _deque&operator=(const)
Deque &deq;// Overload equal sign operator deque.swap(deq); // Interchange vec with its own elements
deque deqIntA,deqIntB,deqIntC,deqIntD; deqIntA.push_back(1);
deqIntA.push_back(3); deqIntA.push_back(5);
deqIntA.push_back(7); deqIntA.push_back(9);
deqIntB.assign(deqIntA.begin(),deqIntA.end()); // 1 3 5 7 9 deqIntC.assign(5,8); //8 8 8 8 8 deqIntD = deqIntA; //1 3 5 7 9 deqIntC.swap(deqIntD); //Exchange deque's size theory. Size ();
//Returns the number of elements in the container deque.empty(); // Determine if the container is empty deque.resize(num);
//Reassign the length of the container to num, and fill the new location with the default value if the container becomes longer. If the container is shorter, elements that end beyond the length of the container are deleted.
deque.resize(num, elem);
//Reassign the length of the container to num, or fill the new location with elem values if the container becomes longer. If the container is shorter, elements that end beyond the length of the container are deleted.
deque<int> deqIntA; deqIntA.push_back(1); deqIntA.push_back(3); deqIntA.push_back(5); int iSize = deqIntA.size(); //3 if (!deqIntA.empty()) { deqIntA.resize(5); //1 3 5 0 0 deqIntA.resize(7,1); //1 3 5 0 0 1 1 deqIntA.resize(2); //1 3 } deque Insertion theory of deque.insert(pos,elem);
//Insert a copy of the elem element at the POS position to return the location of the new data. deque.insert(pos,n,elem);
//Insert n elem data at POS position with no return value. deque.insert(pos,beg,end);
//Insert data in [beg, end] interval at pos position with no return value.
deque deqA; deque deqB;
deqA.push_back(1); deqA.push_back(3); deqA.push_back(5);
deqA.push_back(7); deqA.push_back(9);
deqB.push_back(2); deqB.push_back(4); deqB.push_back(6);
deqB.push_back(8); deqA.insert(deqA.begin(), 11); //{11, 1, 3, 5,
7, 9} deqA.insert(deqA.begin()+1,2,33); //{11,33,33,1,3,5,7,9}
deqA.insert(deqA.begin() , deqB.begin() , deqB.end()
); // Deletion Theory of {2,4,6,8,11,33,33,1,3,5,7,9} deque
deque.clear(); // Remove all data from the container. Erase (beg, end);
//Delete the data in the [beg, end] interval to return to the location of the next data. deque.erase(pos);
//Delete the data at the pos location and return to the location of the next data.
The element deqInt within the deletion interval, declared as a container by deque, now contains 1,3,5,6,9 elements in sequence.
deque::iterator itBegin=deqInt.begin()+1; deque::iterator
itEnd=deqInt.begin()+3; deqInt.erase(itBegin,itEnd);
//The container deqInt now contains 1,6,9 elements in sequence.
Assuming deqInt contains 1,3,2,3,3,3,4,3,5,3, delete the element for(deque::iterator) equal to 3 in the container
It=deqInt. Is(); It!= DeqInt. End (); // No need to write ++ it {if (*it) in parentheses
== 3) {
it = deqInt.erase(it); // With the iterator as a parameter, delete element 3 and return the position of the next element after data deletion to the iterator.
//At this time, do not execute +it;} else {
++it; } }
//Delete all elements of deqInt.clear(); // Container is empty
```cpp ```cpp > #include "iostream" > #include "vector" > #include "deque" > #include "algorithm" using namespace std; > > > > void printD(deque<int> &d) { for (deque<int>::iterator it = > d.begin(); it != d.end(); it++) { cout << *it <<" "; } } > > void main412() { deque<int> d1; ///tail interpolation d1.push_back(1); > d1.push_back(3); d1.push_back(5); > > ///Head Interpolation d1.push_front(-11); d1.push_front(-33); > d1.push_front(-55); > > cout << "Header element" << d1.front() << endl; cout << "Tail element" << d1.back() << > endl; > > printD(d1); cout << endl; //Pop-up elements are equivalent to deleting d1.pop_front(); > d1.pop_back(); printD(d1); > > ///Find-33 at the bottom of the array Deque<int>:: iterator it=find(d1.begin(), d1. End(), > -33); if (it != d1.end()) { cout << "-33 Array subscript is:" << distance(d1.begin(), it) << endl; } else { cout << "Can't find" << > endl; } system("pause"); }
#include "iostream" #include "vector" #include "deque" #include "algorithm" using namespace std; void printD(deque<int> &d) { for (deque<int>::iterator it = d.begin(); it != d.end(); it++) { cout << *it <<" "; } } void main412() { deque<int> d1; ///tail interpolation d1.push_back(1); d1.push_back(3); d1.push_back(5); ///Head Interpolation d1.push_front(-11); d1.push_front(-33); d1.push_front(-55); cout << "Header element" << d1.front() << endl; cout << "Tail element" << d1.back() << endl; printD(d1); cout << endl; //Pop-up elements are equivalent to deleting d1.pop_front(); d1.pop_back(); printD(d1); ///Find-33 at the bottom of the array deque<int>::iterator it=find(d1.begin(), d1.end(), -33); if (it != d1.end()) { cout << "-33 Array subscript is:" << distance(d1.begin(), it) << endl; } else { cout << "Can't find" << endl; } system("pause"); }