ART Education

1 Post

pg.jpg

 

 

http://www.programminggallery.com/home

 

http://www.facebook.com/programminggallery

 

http://www.programminggallery.com/article/c__/stl_libraries/references___deque/stl__deque5#.Ut4W4xC6


Deque

Deque are a kind of sequence container that also can be pronounced as double ended queue. Deque elements can be accessed by the using the iterators same as like the vectors elements also can be accessed by using the index same as like the vectors. The memory allocation for the elements inserting or anything will be handed automatically.

Even though the deque is having almost all the methods and types similar to vectors , the internal implementation is different . For example the vectors are allocated in contiguous memory locations but for deque are allocate the elements in different memory locations and then using an internal mechanism in the class to access the elements. Also they will grow as per the requirement if necessary.

Also the dques are more efficient in allocation compared to the vectors are it avoid the massive reallocation of the elements while on the time of insertion.

If your requirement is to store a large no of elements better to use deque or if you need a data structure that will follow the fifo method the deque is the better option.

Using deque

Header

 

#include <deque>

 

The following is the implementation in the std library of vector

template < class T, class Allocator = allocator<T> > class deque;

 

and the deque parameters are

 

T: Type of the elements.

Allocator:       Type of the allocator object used to define the storage allocation model. By default, the allocator class template for type T is used, which defines the simplest memory allocation model and is value-independent.

 

 

Member Functions

 

 

 

 

Constructor

list::list();

Destructor

~List::list()

Member Functions

 

 

operator=

 

list::begin()

 

list::end()

 

list::rbegin()

 

list::rend()

 

list::empty()

 

list::size()

 

list::max_size()

 

list::front()

 

list::back()

 

list::assign()

 

list::push_front()

 

list::pop_front()

 

list::push_back()

 

list::pop_back()

 

list::insert()

 

list::erase()

 

list::swap()

 

list::resize()

 

list::clear()

 

list::splice()

 

list::remove()

 

list::remove_if()

 

list::unque()

 

list::merge()

 

list::sort()

 

list::reverse()

 

 

 

 

 

Constructor

 

deque( ); explicit deque( const Allocator& _Al);

explicit deque( size_type _Count);

deque(size_type _Count,const Type& _Val);

deque( size_type _Count,const Type& _Val,const Allocator& _Al);

deque(const _deque& _Right);

template<class InputIterator>

   deque(InputIterator _First,InputIterator _Last);

template<class InputIterator>

   deque(InputIterator _First,InputIterator _Last,const Allocator& _Al);

 

Example

 

#include<iostream>

#include<deque>

using namespace std;

 

class TestVectorclass

{

private:

      int num1,num2;

public:

      void Getvals(int arg1,int arg2)

      {

num1 = arg1;

num2 = arg2;}

      void Dispvals()

      {

cout<<" The values num1 , num2 = "<<num1 << " " <<num2 <<"\n";

      }

 

};

void main()

{

      deque<int> v_ITest;

      deque <TestVectorclass> v_ClassTest;

      int testarr[] = {3,4,5,6,7};

      deque<int> v_ITest2 (testarr, testarr + sizeof(testarr) / sizeof(int) );

 

 

      for(int i = 0; i<10; i++)

v_ITest.push_back(i);

 

      deque<int>::iterator it;

      cout<<" values from v_ITest\n";

 

      for(it = v_ITest.begin(); it <v_ITest.end(); it++)

cout<<*it <<"\n";

 

      cout<<" values from v_ITest2\n";

 

      for(it = v_ITest2.begin(); it <v_ITest2.end(); it++)

cout<<*it <<"\n";

 

      TestVectorclass testclass;

      TestVectorclass testclass1;

      testclass.Getvals(1,2);

      testclass1.Getvals(3,4);

 

v_ClassTest.push_back(testclass);

v_ClassTest.push_back(testclass1);

 

deque<TestVectorclass>::iterator itclass;

 

      for(itclass = v_ClassTest.begin(); itclass <v_ClassTest.end(); itclass++)

(*itclass).Dispvals();

}

 

Destructor

deque::~ deque ()

Destruct the deque object and dealloctes all the member elements.

 

Member Functions

Returns the iterator reference to the begin or the first element of the deque.

Example

#include<iostream>

#include<deque>

using namespace std;

 

void main()

{

deque<int> v_ITest;

 

for(int i = 0; i<10; i++)

      v_ITest.push_back(i);

 

deque<int>::iterator it;

cout<<" values from v_ITest\n";

// begin sample

for(it = v_ITest.begin(); it <v_ITest.end(); it++)

      cout<<*it <<"\n";

 

cout<<" values from v_ITest2\n";

 

}

 

deque::end()

Returns the iterator reference to the end or the last element of the deque.

Example

#include<iostream>

#include<deque>

using namespace std;

 

void main()

{

      deque<int> v_ITest;

 

      for(int i = 0; i<10; i++)

v_ITest.push_back(i);

 

      deque<int>::iterator it;

      cout<<" values from v_ITest\n";

      // end sample

      for(it = v_ITest.begin(); it <v_ITest.end(); it++)

cout<<*it <<"\n";

}

 

deque::rbegin()

Returns the reverse iterator to the last element in the deque

Example

#include<iostream>

#include<deque>

using namespace std;

 

void main()

{

      deque<int> v_ITest;

 

      for(int i = 0; i<10; i++)

v_ITest.push_back(i);

 

      // get reverse iterator reference

 

      deque<int>::reverse_iterator it;

      cout<<" values from v_ITest\n";

      // rbegin

      for(it = v_ITest.rbegin(); it < v_ITest.rend(); ++it)

cout<<*it <<"\n";

}

deque::rend()

Returns the reverse iterator to the first element in the deque

Example

#include<iostream>

#include<deque>

using namespace std;

 

void main()

{

      deque<int> v_ITest;

 

      for(int i = 0; i<10; i++)

v_ITest.push_back(i);

 

      // get reverse iterator reference

 

      deque<int>::reverse_iterator it;

      cout<<" values from v_ITest\n";

      // rend

      for(it = v_ITest.rbegin(); it < v_ITest.rend(); ++it)

cout<<*it <<"\n";

}

 

deque::size()

Returns the size of elements inside the deque.

Example

#include<iostream>

#include<deque>

using namespace std;

 

void main()

{

      deque<int> v_ITest;

 

      for(int i = 0; i<10; i++)

v_ITest.push_back(i);

 

      // get the size and iterate

 

          

      cout<<" values from v_ITest\n";

    

      for(int i = 0;i <v_ITest.size();i++)

cout<<v_ITest[i]<<"\n";

}

 

deque::max_size()

This will return the maximum size of the elements that the vector element can hold. Ie the maximum potential size the deque can reach .

Example

#include<iostream>

#include<deque>

using namespace std;

 

void main()

{

      deque<int> v_ITest;

 

      for(int i = 0; i<10; i++)

v_ITest.push_back(i);

 

      // get the size and iterate

 

          

      cout<<"maximum size for v_ITest " << v_ITest.max_size() <<"\n";

    

}

 

deque::resize()

Syntax

void resize ( size_type sz, T c = T() );

Parameters

sz

New deque size, expressed in elements.

Member type size_type is an unsigned integral type.

c que

Object whose content is copied to the added elements in case that sz is greater than the
current deque size.

If not specified, the default constructor is used.
T is the first template parameter (the type of the elements stored in the de).

this function resizes the deque size to the the new size specified. This function will change the actual content of the deque by inserting or removing the elements from the deque

Example

 

#include<iostream>

#include<deque>

using namespace std;

 

void main()

{

      deque<int> v_ITest;

 

      for(int i = 0; i<10; i++)

v_ITest.push_back(i);

 

      cout << "v_ITest contains:\n";

    for (int i=0;i <(int) v_ITest.size();i++)

       cout << " " << v_ITest[i]<<"\n";

 

      // resize the vector

 

      v_ITest.resize(4);

          

cout << "\nv_ITest contains after resize:\n";

for (int i=0;i< (int)v_ITest.size();i++)

    cout << " " << v_ITest[i]<<"\n";

}

 

deque::empty ()

This member function return a bool value weather the deque is empty or not

Example

#include<iostream>

#include<deque>

using namespace std;

 

void main()

{

      deque<int> v_ITest;

      // check vector is empty

 

      if(v_ITest.empty())

      {

      for(int i = 0; i<10; i++)

v_ITest.push_back(i);

      }

      cout << "v_ITest contains:\n";

    for (int i=0;i <(int) v_ITest.size();i++)

       cout << " " << v_ITest[i]<<"\n";

 

}

 

deque::operator[n]

returns the reference to the element at the position .

Example

#include <iostream>

#include <deque>

using namespace std;

 

int main ()

{

deque<int> sample1(10);

 

for(int i = 0; i<sample1.size(); i++)

{

       // assign the element

      sample1[i] = i;

}

 

for(int i = 0 ;i<sample1.size() ; i++)

{

       // get the element

      cout<< sample1[i];

 

}

}

 

deque::at()

returns the reference to the element at the position .

Example

#include <iostream>

#include <deque>

using namespace std;

 

int main ()

{

deque<int> sample1(10);

 

for(int i = 0; i<sample1.size(); i++)

{

       // assign the element

      sample1[i] = i;

}

 

for(int i = 0 ;i<sample1.size() ; i++)

{

       // get the element

      cout<< sample1.at(i);

 

}

}

 

deque::front()

Returns a reference to the first element on the deque

Example

#include <iostream>

#include <deque>

using namespace std;

 

int main ()

{

deque<int> sample1;

 

for(int i = 2; i<10; i++)

{

       // assign the element

      sample1.push_back(i);

}

 

cout<<"element at front "<<sample1.front()<<"\n";

 

}

 

deque::back()

Returns a reference to the last element in the deque

Example

#include <iostream>

#include <deque>

using namespace std;

 

int main ()

{

deque<int> sample1;

 

for(int i = 2; i<10; i++)

{

       // assign the element

      sample1.push_back(i);

}

 

cout<<"element at front "<<sample1.back()<<"\n";

 

}

 

deque::assign()

Assigns a new content to the deque object. This methord will drop all elements in the container and replaces with the new elements in specified in the parameter.

Example

#include <iostream>

#include <deque>

using namespace std;

 

int main ()

{

       deque<int> sample1;

       deque<int> sample2;

 

       sample1.assign(5,100);

       sample2.assign(3,20);

 

      deque<int>::iterator it;

 

      // begin sample

      cout<<"before assign \n";

 

      for(it = sample1.begin(); it <sample1.end(); it++)

cout<<*it <<"\n";

      

sample1.assign(sample2.begin(),sample2.end());

 

      cout<<"after asign   \n";

      for(it = sample1.begin(); it <sample1.end(); it++)

cout<<*it <<"\n";

 

}

 

deque::push_back()

This will add the element on the end of the deque and insertion will effect in increse In size and deallocate the iterators.

Example

#include<iostream>

#include<deque>

using namespace std;

 

void main()

{

deque<int> v_ITest;

 

for(int i = 0; i<10; i++)

      v_ITest.push_back(i);// push_back

 

deque<int>::iterator it;

cout<<" values from v_ITest\n";

 

for(it = v_ITest.begin(); it <v_ITest.end(); it++)

      cout<<*it <<"\n";

 

}

 

deque::push_front()

This will add the element on the begining of the deque and insertion will effect in increse In size and deallocate the iterators.

#include<iostream>

#include<deque>

using namespace std;

 

void main()

{

deque<int> v_ITest;

 

for(int i = 0; i<10; i++)

      v_ITest.push_front(i);// push_back

 

deque<int>::iterator it;

cout<<" values from v_ITest\n";

 

for(it = v_ITest.begin(); it <v_ITest.end(); it++)

      cout<<*it <<"\n";

 

}

 

deque::pop_back

This function removes the last element from the deque and reduce the size of the vector by one  and also it will call the destructor of elemnt inside the deque that is removed.

Example

#include<iostream>

#include<deque>

using namespace std;

 

void main()

{

      deque<int> v_ITest;

 

      for(int i = 0; i<10; i++)

v_ITest.push_back(i);// push_back

 

 

      // pop_back element

      while(!v_ITest.empty())

      {

      cout<<"element going to delatedis " <<v_ITest.back() <<"size is "<<v_ITest.size()<<"\n";

      v_ITest.pop_back();

 

      }

 

      cout<<"all elements deleted\n";

}

 

deque::pop_front

This function removes the first element from the deque and reduce the size of the vector by one and also it will call the destructor of element inside deque that is removed.

Example

 

#include<iostream>

#include<deque>

using namespace std;

 

void main()

{

      deque<int> v_ITest;

 

      for(int i = 0; i<10; i++)

v_ITest.push_back(i);// push_back

 

 

      // pop_back element

      while(!v_ITest.empty())

      {

      cout<<"element going to delated is " <<v_ITest.front() <<"size is "<<v_ITest.size()<<"\n";

      v_ITest.pop_front();

 

      }

 

      cout<<"all elements deleted\n";

}

 

deque::insert

Syntax

iterator insert ( iterator position, const T& x );
void insert ( iterator position, size_type n, const T& x );
template <class InputIterator>
void insert ( iterator position, InputIterator first, InputIterator last );

Parameters

position

Position in the vector where the new elements are inserted.
iterator is a member type, defined as a random access iterator type.

x

Value to be used to initialize the inserted elements.
T is the first template parameter (the type of the elements stored in the vector).

n

Number of elements to insert. Each element is initialized to the value specified in x.
Member type size_type is an unsigned integral type.

first, last

Iterators specifying a range of elements. Copies of the elements in the range [first,last) are inserted at position position.

Notice that the range includes all the elements between first and last, including the element pointed by first but not the one pointed by last.

The template type can be any type of input iterator.

This methord insert the element with a specified location. Also will increase the vector size by one.

Exsample

#include<iostream>

#include<deque>

using namespace std;

 

void main()

{

      deque<int> v_ITest;

      for(int i=0;i<=10;i++)

      v_ITest.push_back(i);

    

      deque<int>::iterator it;

 

cout<<"\n";

 

      it= v_ITest.end();

      v_ITest.insert(it,11);// insert at a specified location

    

      // insertanother vector v_ITest1r1

deque<int> v_ITest1;

      for(int i=12;i<=22;i++)

      v_ITest1.push_back(i);

 

      it = v_ITest.end();

v_ITest.insert(it,v_ITest1.begin(),v_ITest1.end());

 

    

for(it = v_ITest.begin();it<v_ITest.end();it++)

      {   

cout<<*it<<"\n" ;

      }

    

}

 

deque::erase

This methord erases a particular element from a position or erases elements from a range of position specified by the function.

 

Example

#include<iostream>

#include<deque>

using namespace std;

 

void main()

{

deque<int> v_ITest;

            for(int i=0;i<=10;i++)

v_ITest.push_back(i);

          

deque<int>::iterator it;

 

cout<<"\n";

 

it= v_ITest.end();

v_ITest.insert(it,11);// insert at a specified location

          

            // insertanother vector v_ITest1r1

      deque<int> v_ITest1;

            for(int i=12;i<=22;i++)

v_ITest1.push_back(i);

 

it = v_ITest.end();

v_ITest.insert(it,v_ITest1.begin(),v_ITest1.end());

      cout<<"before deleted \n";

      for(it = v_ITest.begin();it<v_ITest.end();it++)

{   

cout<<*it<<"\n" ;

}

      it = v_ITest.begin();

 

      v_ITest.erase(it);

 

      cout<<"deleted the last item from end\n";

 

      for(it = v_ITest.begin();it<v_ITest.end();it++)

{   

cout<<*it<<"\n" ;

}

 

cout<<"deleted the first 5 elements remaining\n";

 

      it = v_ITest.begin();

          

      v_ITest.erase(it,it+5);

      for(it = v_ITest.begin();it<v_ITest.end();it++)

{   

cout<<*it<<"\n" ;

}

}

 

deque::swap

This function inter exchanges the contents of a deque to an another deque.

#include<iostream>

#include<deque>

using namespace std;

 

void main()

{

deque<int> v_ITest;

            for(int i=0;i<=10;i++)

v_ITest.push_back(i);

                      

            // insertanother vector v_ITest1r1

deque<int> v_ITest1;

            for(int i=0;i<=22;i++)

v_ITest1.push_back(i);

 

cout<<"before swap\n";

 

cout<<"array 1\n";

            for(deque<int>::iterator it = v_ITest.begin();it<v_ITest.end();it++)

{   

cout<<*it<<"\n" ;

}

cout<<"array 2\n";

    

            for(deque<int>::iterator it = v_ITest1.begin();it<v_ITest1.end();it++)

{   

cout<<*it<<"\n" ;

}

 

cout<<"after swap\n";

 

v_ITest.swap(v_ITest1);

          

cout<<"array 1\n";

            for(deque<int>::iterator it = v_ITest.begin();it<v_ITest.end();it++)

{   

cout<<*it<<"\n" ;

}

cout<<"array 2\n";

    

            for(deque<int>::iterator it = v_ITest1.begin();it<v_ITest1.end();it++)

{   

cout<<*it<<"\n" ;

}

}

 

 

deque::clear

This method clears the content of the deque and also will call the destructors of the elements inside

#include<iostream>

#include<deque>

using namespace std;

 

void main()

{

deque<int> v_ITest;

            for(int i=0;i<=10;i++)

v_ITest.push_back(i);

 

v_ITest.clear();

cout<< "size of the vector " <<v_ITest.size() <<"\n";

}

 

Any questions please let us know


http://www.programminggallery.com/home

 

http://www.facebook.com/programminggallery

 

http://www.programminggallery.com/article/c__/stl_libraries/references___deque/stl__deque5#.Ut4W4xC6