Deque double-ended queue container (object creation, array, iterator access, element assignment, insertion, deletion, etc.)

Source: Internet
Author: User

Deque is very similar to vectors, where you can insert and delete elements at the end, as well as insert and delete headers. However, when considering the memory allocation policy and operational performance of the container elements, deque has advantages over vectors.

Header file

#include <deque>

Create a Deque object

1) deque ();// Create a Deque object without any elements .

Deque<int> D

2) deque (size_typen);// Create a Deque object with n elements , each with a default value under its type.

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

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, creates a new One by copying the element values of a Deque object The 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 be defaulted.

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 into the tail, and is commonly used as the initialization assignment for deque containers.

Traversal access of elements

The deque element is accessed using array and iterator methods 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 mode access to 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 Access deque element:" <<endl;for (I=0,j=d.begin (); j!=jend;i++,j++) {cout << "d[" << i << "] =" << *j << Endl;} return 0;}


Insertion of elements

Since 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

Iteratorinsert (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 a pop_front function to delete the first element , delete the Pop_back function of the trailing element , delete the erase function of the element at any position or iteration interval , and Delete all elements the clear function.

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) Iteratorerase (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;//tail and anywhere 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 <<" execute Clear () "<<endl << "Deque element cleared all" << Endl;return 0;}

Other functions are similar to vector containers and are not detailed here, but can be seen in the application of the previous vector.


This article for my original, reproduced please indicate the source: http://blog.csdn.net/lsh_2013/article/details/46737877







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

Deque double-ended queue container (object creation, array, iterator access, element assignment, insert, delete, etc.)

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.