STL Learning Series III: Deque container

Source: Internet
Author: User

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>using namespacestd; #include<deque>voidobjPlay3 () {deque<int>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 9deque<int> Deqintc (5,8);//8 8 8) 8 8deque<int> Deqintd (Deqinta);//1 3 5) 7 9}intMain () {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 (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 9Deqintc.assign (5,8);//8 8 8) 8 8Deqintd = Deqinta;//1 3 5) 7 9Deqintc.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

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.
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.

STL Learning Series III: Deque container

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.