Stl-deque (double-ended queue)

Source: Internet
Author: User

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.
#include <deque>
Default constructs for Deque objects
Deque is implemented by template class, and the default structure of Deque object is:deque<t> deqt;
Deque <int> Deqint; A deque container that holds int.
Deque <float> DEQ float; A deque container that holds float.
Deque <string> DEQ string; A deque container that holds a string.
...
You can also set pointer types or custom types within angle brackets.

Add remove action at end of Deque
Theoretical knowledge:
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

<span style= "White-space:pre" ></span>deque<int> deqint;deqint.push_back (1);d Eqint.push_back (3) ;d Eqint.push_back (5);d Eqint.push_back (7);d Eqint.push_back (9);d Eqint.pop_front ();d eqint.pop_front ();d Eqint.push _front (one);d Eqint.push_front ();d eqint.pop_back ();d eqint.pop_back ();//deqint  {13,11,5}

Deque data Access
Theoretical knowledge:
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
<span style= "White-space:pre" ></span>deque<int> deqint;deqint.push_back (1);d Eqint.push_back (3) ;d Eqint.push_back (5);d Eqint.push_back (7);d Eqint.push_back (9); int iA = deqint.at (0);//1int IB = deqint[1];// 3deqint.at (0) = 99;//99deqint[1] = 88;//88int Ifront = Deqint.front ();//99int iback = Deqint.back ();//9deqInt.front () = 77 ;//77deqint.back () = 66;//66

Deque and Iterators
Theoretical knowledge
Deque.begin (); Returns an iterator to the first element in a container.
Deque.end (); Returns an iterator after the last element in the container.
Deque.rbegin (); Returns an iterator to the first element in the container.
Deque.rend (); Returns an iterator after the last element in the container.

<span style= "White-space:pre" ></span>deque<int> deqint;deqint.push_back (1);d Eqint.push_back (3) ;d Eqint.push_back (5);d Eqint.push_back (7);d Eqint.push_back (9); for (Deque<int>::iterator It=deqint.begin (); It!=deqint.end (); ++it) {cout << *it;cout << "";} 1 3 5 7 9for (Deque<int>::reverse_iterator rit=deqint.rbegin (); Rit!=deqint.rend (); ++rit) {cout << *rit;co UT << "";} 9 7 5) 3 1

Construction of Deque object with parameters
Theoretical knowledge
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 &AMP;DEQ); Copy constructor.
<span style= "White-space:pre" ></span>deque<int> deqinta;deqinta.push_back (1);d Eqinta.push_back (3);d Eqinta.push_back (5);d Eqinta.push_back (7);d Eqinta.push_back (9);d eque<int> deqintb (Deqinta.begin (), Deqinta.end ());//1 3 5 7 9deque<int> DEQINTC (5,8);//8 8 8 8 8deque<int> deqintd (Deqinta);//1 3 5 7 9

Assignment of Deque
Theoretical knowledge
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 &AMP;DEQ); Overloaded equals operator
Deque.swap (DEQ); To swap the VEC with its own elements
<span style= "White-space:pre" ></span>deque<int> deqinta,deqintb,deqintc,deqintd;deqinta.push_ Back (1);d Eqinta.push_back (3);d Eqinta.push_back (5);d Eqinta.push_back (7);d Eqinta.push_back (9);d eqintb.assign ( Deqinta.begin (), Deqinta.end ());//1 3 5 7 9deqintc.assign (5,8);//8 8 8 8 8deqIntD = DEQINTA;//1 3 5 7 9deqintc.swap (deqint D);//Interchange

Size of the Deque
Theoretical knowledge
Deque.size (); Returns the number of elements in a container
Deque.empty (); Determine if 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.
<span style= "White-space:pre" ></span>deque<int> deqinta;deqinta.push_back (1);d Eqinta.push_back (3);d Eqinta.push_back (5); int isize = Deqinta.size ();  3if (!deqinta.empty ()) {deqinta.resize (5);//1 3 5 0 0deqinta.resize (7,1);//1 3 5 0 0 1 1deqinta.resize (2);//1 3}

Insertion of Deque
Theoretical knowledge
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.
<span style= "White-space:pre" ></span>deque<int> deqa;deque<int> deqb;deqa.push_back (1); Deqa.push_back (3);d Eqa.push_back (5);d Eqa.push_back (7);d Eqa.push_back (9);d Eqb.push_back (2);d eqb.push_back (4); Deqb.push_back (6);d Eqb.push_back (8);d Eqa.insert (Deqa.begin (), one),//{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}

Deletion of Deque
Theoretical knowledge
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.
The element deqint in the delete interval is a container declared with deque<int>, and now contains an ordered 1,3,5,6,9 element. Deque<int>::iterator itbegin=deqint.begin () +1;deque<int>::iterator itEnd=deqInt.begin () +3; Deqint.erase (itbegin,itend);//At this time the container deqint contains 1,6,9 three elements sequentially. Assuming that deqint contains 1,3,2,3,3,3,4,3,5,3, delete the element in the container equal to 3 for (Deque<int>::iterator it=deqint.being (); It!=deqint.end ();)    The parentheses do not need to write  ++it{   if (*it = = 3)   {        It  =  deqint.erase (it);       The iterator is the parameter, the element 3 is removed, and the next element position after the data is deleted is returned to the iterator.         //At this time, do not execute  ++it;     }   else   {       ++it;}   } Delete all elements of Deqint deqint.clear ();//Container is empty

Demo

#include <iostream> #include <cstdio> #include <deque> #include <algorithm>using namespace std; void Printdeque (deque<int> &d) {for (Deque<int>::iterator it = D.begin (); It! = D.end (); it++) {cout <&L T *it << ';} cout << Endl;} void Dequeinit () {deque<int> d1;d1.push_back (1);d 1.push_back (3);d 1.push_back (5);d 1.push_front ( -11);d 1.push_ Front ( -33);d 1.push_front ( -55);p rintdeque (D1);// -55-33-11 1 3 5cout << "front element:" << D1.front () <& Lt endl;//front element: -55cout << "back element:" << d1.back () << endl;//back Element:5d1.pop_front () ;d 1.pop_back ();p rintdeque (D1);//-33-11 1 3//find 1 location in D1 deque<int>::iterator it = Find (D1.begin (), D1.end (), 1); if (it = D1.end ()) {cout << "position of 1:" << distance (D1.begin (), it) << Endl;} else {cout << "no 1\n";} Position of 1:2}int Main () {dequeinit (); return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Stl-deque (double-ended queue)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.