STL's Deque double-ended queue container

Source: Internet
Author: User

Deque is very similar to vectors, not only to insert and delete elements at the tail, but also to insert and delete them in the head.

Only when considering the memory allocation policy and operational performance of the container element. Deque has advantages over vectors.

Header file

#include <deque>

Create a Deque object

1) deque ();// Create a Deque object that does not have any elements whatsoever .

Deque<int> D

2) deque (size_typen);// Create a Deque object with n elements . Each element adopts the default value under its type.

Deque<int> d (x);//deque Object D has 10 elements, and each element has an initial value of 0.

3) deque<size_type N, constt& value); creates a deque object with n elements with an initial value of values .

Deque<double> d (10,5);

4) deque (const deque&) ,//deque copy constructor, copies the element value of a Deque object. Creates a new deque object.

deque<char> D1 (5, ' a ');d eque<char> D2 (D1);

5) deque (constinputiterator first, const inputiterator last, const a& a=a ());

// the Iteration interval [First,last] The element that is referred to is copied to a newly created deque object, where the memory allocator can default.

Using the int array iarray, create a Deque object dint iarray[]={1,2,3,4,5,6,7};d eque<int> D (IArray, iarray+7);
Initialize Assignment

Using the push_back () function provided by deque, the new element value can be pressed at the tail. It is often used as an initialization assignment for deque containers.

Traversal of elements

Access the Deque element in array mode and iterator mode , respectively.

#include <deque> #include <iostream>using namespace std; int main (void) {deque<int> d;int i;d.push_back (;d). Push_back (+);d. Push_back " Array way to access the deque element: "<<endl;for (i=0; i < d.size (); i++) cout <<" d["<< i <<"] = "<< D[i] < < Endl;  Deque<int>::iterator j,jend;//defines the iterator jend=d.end ();cout<< "iterator to the deque element:" <<endl;for (I=0,j=d.begin (); j!=jend;i++,j++) {cout << "d[" << i << "] =" << *j << Endl;} return 0;}


Insertion of elements

Because Deque uses two iterators to point to the end of a double-ended queue, Deque has a function Push_front ()that has an efficient head insertion element . Insert () function for middle position insertion.

void Push_front (constt&);// Head Insertion

iterator Insert (iterator pos, const t& x);//pos Insert a new element before the position x

#include <deque> #include <iostream>using namespace Std;int main (void) {deque<int> d;d.push_back (6); D.push_back (7);   Head Insert D.push_front (5); for (int i=0; i<d.size (); i++)  //print 6 7cout << d[i] << "; cout << endl;//Middle Position Insertion D.insert (D.begin () +1, 9);     Insert before the first element, i.e. 9 6 7for (int j=0; j<d.size (); j + +) cout << d[j] << "; cout << Endl;return 0;}


Deletion of elements

The Deque container provides the Pop_front function to delete the first element . Deletes the Pop_back function of the tail element . Deletes the erase function of an element on any position or iteration interval . and the clear function to remove all elements .

1) void Pop_front ();// Delete The first element of a deque

2) void Pop_back ();// Delete The last element of deque

3) Iterator erase (Iteratorpos); Delete The element that the POS points to

4) Iterator erase (iterator first, iterator last);// Delete all elements pointed to by the iteration interval [first,last]

5) void clear ();// Delete all elements

#include <deque> #include <iostream>using namespace Std;int main (void) {deque<int> d;d.push_back (4); D.push_back (5);d. push_back (1);d. push_back (1);d. push_back (1);d. push_back (6); for (int i=0; i<d.size (); i++) cout << D[i] << cout << endl;//kinsoku and any location delete element D.erase (D.begin () + 1);d. Pop_front ();d. Pop_back (); for (int j= 0; J<d.size (); J + +) cout << d[j] << "cout << endl;//Delete all elements d.clear (); cout <<" Run Clear () "<<endl << "Deque element all purge" << Endl;return 0;}

Other functions are similar to vector containers and are not mentioned here. The application of the previous vector can be seen in detail.


This article is my original. Reprint Please specify source: http://blog.csdn.net/lsh_2013/article/details/46737877







STL's Deque double-ended queue 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.