1.Deque Introduction
- Deque is the abbreviation for "double-ended queue", which is the same as the vector of the STL, Deque is a double-ended array, and the vector is single-ended.
- The deque is very similar to the vector on the interface and can be replaced directly in many places of operation.
- Deque can randomly access elements (supporting direct access to indexed values, using the [] operator or the at () method, which is described in more detail).
- Deque adding or removing elements from the head and tail are very fast. However, it is time consuming to insert elements or remove elements in the middle.
Default construction of 2.deque objects
Deque is implemented by template class, and the default structure of Deque object is:deque<t> DEQ;
Deque <int> Deqint; A deque container that holds int.
Deque <float> DEQ float; A deque container that holds float.
Parametric construction of 3.deque objects
- Deque (Beg,end); The constructor copies the elements in the [Beg, end] interval to itself. Note that the interval is left-closed and right-open.
- Deque (N,elem); A constructor copies n Elem to itself.
- Deque (const deque &DEQ); Copy constructor.
#include <iostream>UsingNamespaceSTD; #include <deque>voidObjPlay3 () {deque<Int>Deqinta; Deqinta.push_back (1); Deqinta.push_back (3); Deqinta.push_back (5); Deqinta.push_back (79int> 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 3 5 7 9 }int main () {objPlay3 (); return 0
Assignment of 4.deque
- Deque.assign (Beg,end); Assigns 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); Assigns an n elem copy to itself.
- deque& operator= (const deque &DEQ); Overloaded equals operator
- Deque.swap (DEQ); To swap the VEC with its own elements
voidObjPlay4 () {deque<Int>Deqinta, DEQINTB, DEQINTC, Deqintd; Deqinta.push_back (13579// 1 3 5 7 9 deqintc.assign (5, 8); //8 8 8 8 8 deqintd = Deqinta; //1 3 5 7 9 Deqintc.swap (DEQINTD); //c and D interchange}
Size of 5.deque
- Deque.size (); Returns the number of elements in the container.
- Deque.empty (); Determines whether the container is empty.
- Deque.resize (num); The container is re-specified as NUM, and if the container is longer, the new position is populated with the default value. If the container is shorter, the element at the end of the container length is removed.
- Deque.resize (num, elem); Reassign the container's length to num, and if the container is longer, fill the new position with the Elem value. If the container is shorter, the element at the end of the container length is removed.
void ObjPlay5 ()
{
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
}
}
Add remove operation at end of 6.deque
- Deque.push_back (Elem); Add a data to the end of the container
- Deque.push_front (Elem); Inserting a data into the container's head
- Deque.pop_back (); Delete the last data of a container
- Deque.pop_front (); Delete the first data of the container
voidObjPlay6 () {deque<Int>Deqint; Deqint.push_back (1); Deqint.push_back (3); Deqint.push_back (57); Deqint.push_back (9); at this time 1,3,5,7,9 Deqint.pop_front (); // pop-up the first element of the head Deqint.pop_front (); Deqint.push_front ( Span style= "color: #800080;" >11); // header add Element Deqint.push_front ( 13// popup last element Deqint.pop _back ();}
7.deque of data access
- deque.at (IDX); Returns the index IDX refers to the data, if the IDX is out of bounds, throws Out_of_range.
- DEQUE[IDX]; Returns the data that the index IDX refers to, if the IDX is out of bounds and does not throw an exception, a direct error occurs.
- Deque.front (); Returns the first data.
- Deque.back (); Returns the last data
Insertion of 8.deque
- Deque.insert (Pos,elem); Inserts a copy of the Elem element at the POS location, returning the location of the new data.
- Deque.insert (Pos,n,elem); Insert n elem data at POS location, no return value.
- Deque.insert (Pos,beg,end); The data in the [Beg,end] interval is inserted at the POS location, with no return value.
voidObjPlay8 () {deque<Int>Deqa; deque<Int>DEQB; Deqa.push_back (1); Deqa.push_back (3); Deqa.push_back (5); Deqa.push_back (7); Deqa.push_back (9); Deqb.push_back (24< Span style= "color: #000000;" >); Deqb.push_back (68); 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 ()); //{2,4,6,8,11,33,33,1,3,5,7,9} }
Removal of 9.deque
- Deque.clear (); Remove all data from a container
- Deque.erase (Beg,end); Deletes the data for the [Beg,end] interval, returning the position of the next data.
- Deque.erase (POS); Deletes the data at the POS location and returns the location of the next data.
voidObjPlay9 () {int intarr[10] = {1,3,2,3,3,3,4,3,5,3};//Suppose Deqint contains 1, 3, 2, 3, 3, 3, 4, 3, 5, 3, removing the element that is equal to 3 in the container deque<Int> Deqint (intarr, Intarr +10);for (deque<int>::iterator it = Deqint.begin (); It! = Deqint.end ();) // {if (*it = = 3// takes an iterator as a parameter, removes element 3, and returns the next element position after the data is deleted to the iterator. // at this time, do not perform ++it; else {++it;}} // delete all deqint elements deqint.clear (); //"
STL Learning Three: Deque container