Deque Dual-ended queue container

Source: Internet
Author: User

C + + in the STL is still more useful, especially in the implementation of scientific research algorithms, but also useful, but not how the system has been studied, so recently looked for a book, Ye Zhijun the "C + + STL development Technology Guidance", science, the nature of a book, written relatively shallow [hehe, do not spray ]。 Most of the content below is excerpted from the book.
Deque double-ended queue container (double-ended queue), can be inserted in the tail, head, delete elements, the use of a block of linear structure to store data, two iterators point to the first and the end of the container, the deque block for memory allocation, using a two-level map for management.

Create a Deque object

There are several ways to create the Deque container object.
(1) deque ()
The Deque container created in this way already has an object, except that the tail-to-kinsoku iterator overlaps, and there are no elements in the container.

deque<int> d;

(2) Deque (size_type N)
Creates a Deque object with n elements, each with a default value of the corresponding type.

deque<int> d(10);//10个元素的初始值为0

(3) deque (size_type n,const t& value)
Creates a Deque object with n elements with an initial value of values.

deque<double> d(10,2.5);

(4) deque (const deque&)
Copy constructor.

deque<char> d1(5,’k’);deque<char> d2(d1);

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

intarray[]={1,2,3,4,5};deque<int>d(array,array+5);
Deque initialization

Use the Push_back function to press the new element at the tail.

deque<int> d;d.push_back(10);
Traversal access of elements

can be accessed in the form of arrays and iterators.
To access the Deque element using an array method:

#include <iostream>#include <deque>using namespace STD;intMain () { deque<int>D D.push_back (1); D.push_back (2); D.push_back (3); D.push_back (4); for(intI=0; I<d.size (); i++) {cout<<"d["<<i<<"]="<<d[i]<<endl; }return 0;}

To access the Deque element with an iterator:

#include <iostream>#include <deque>using namespace STD;intMain () { deque<int>D D.push_back (1); D.push_back (2); D.push_back (3); D.push_back (4); deque<int>:: Iterator Begin,end; End=d.end ();intI for(Begin=d.begin (), i=0; begin!=end;begin++,i++) {cout<<*begin<<endl; }return 0;}
Insertion of elements

Deque uses two iterators to point to the end of a double-ended queue, Deque has a function push_front () with an efficient head insertion element, and its prototype is:

void push_front(const T&);

The insertion of other locations involves a shift copy of the element, and the Insert function inserts the element x before the POS position,

Iterator insert(iterator pos,const T& x);

Cases:

#include <iostream>#include <deque>using namespace STD;intMain () { deque<int>D D.push_back (1); D.push_back (2);//Head insertionD.push_front (3); for(intI=0; I<d.size (); i++) {cout<<d[i]<<" "; }cout<<endl;//Middle position insertionD.insert (D.begin () +2,4);//Insert 4 before the 3rd element     for(intI=0; I<d.size (); i++)cout<<d[i]<<" ";cout<<endl;return 0;}
Deletion of elements

Delete the first element Pop_front function, delete the tail element pop_back function, delete any position or iteration interval erase function, delete all element clear function.

#include <iostream>#include <deque>using namespace STD;intMain () { deque<int>D D.push_back (1); D.push_back (2); D.push_back (3); D.push_back (4); D.push_back (5); for(intI=0; I<d.size (); i++) {cout<<d[i]<<" "; }cout<<endl;//tail and anywhere delete elementD.erase (D.begin () +2);//Delete element 3rdD.pop_front (); D.pop_back (); for(intI=0; I<d.size (); i++) {cout<<d[i]<<" "; }cout<<endl; D.clear ();//Delete all elements    cout<<d.size () <<endl;return 0;}
Reverse traversal of elements

Reverse iterators reverse_iterator and const_reverse_iterator.

#include <iostream>#include <deque>using namespace STD;intMain () { deque<int>D D.push_back (1); D.push_back (2); D.push_back (3); D.push_back (4); D.push_back (5);//forward traversal     deque<int>:: Iterator Begin,end; End=d.end (); for(Begin=d.begin (); begin!=end;begin++)cout<<*begin<<" ";cout<<endl;//Reverse Traversal     deque<int>:: Reverse_iterator rbegin,rend; Rend=d.rend (); for(Rbegin=d.rbegin (); rbegin!=rend;rbegin++)cout<<*rbegin<<" ";cout<<endl;return 0;}
Exchange of Deque

Swap with swap functions.

#include <iostream>#include <deque>using namespace STD;voidPrint deque<int>&AMP;D) { for(intI=0; I<d.size (); i++)cout<<d[i]<<" ";cout<<endl;}intMain () {//d1     deque<int>D1; D1.push_back (1); D1.push_back (2);cout<<"Before-----------Exchange:"<<endl;cout<<"d1="; Print (D1);//d2     deque<int>D2; D2.push_back (3); D2.push_back (4); D2.push_back (5);cout<<"d2="; Print (D2);cout<<"After-----------Exchange:"<<endl; D1.swap (D2);cout<<"d1="; Print (D1);cout<<"d2="; Print (D2);return 0;}

Other functions

BOOL Empty ()
Size_type size ()
Size_type max_size ()
Reference Front ()
Reference back ()

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

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