Using deque class

deque is a collection or container class that allows you to store and process any type of values. For applications that require frequently insertion and deletion at the front and back of a container, deque class is efficient.
The deque class has the following constructors that can be used to create a deque object to store and process values.
-deque(const allocator_type& __a) creates an empty deque object with the default allocator.
-deque(size_type __n) creates a deque object with specifying the number of items to be stored in that container.
-deque(size_type __n, const _Tp& __value, const allocator_type& __a) creates an deque object with specifying the number of items to be stored in that container, and fills that container with n copies of value.
-deque(const deque& __x) creates a deque object from a specified deque object.

The commonly used member functions of the deque class are push_back(), push_front(), pop_back(), pop_front(), clear(), begin(), end(), and size(). The push_back() and push_front() allow you to add a new item to end and to the beginning of the deque respectively. In contrast, the pop_back() and pop_font() will remove the last item and the beginning item from the deque respectively. By using the clear(), you can delete all items of the deque. The begin() reads the first item and the end() refers to the next position after the end of the container. The size() member returns the number of items stored in the deque.
Other functions that are frequently used to operate on the deque container are sort(), reverse(), find(), copy(), min_element(), and max_element. The functions are not members of the deque. They are defined in the stl_algo.h header file. You can not only use these functions with the deque class but also with other container classes. Below is the example code that uses these functions.

#include <cstdlib>
#include <iostream>
#include <deque>
#include <iterator>
using namespace std;
//function predicate used to specify sort order
bool com(int a,int b){
   if(a>=b) return true;
   else return false;

}

int main(int argc, char *argv[])
{
   //create an empty deque object
   deque<int> dq;
   //create output stream iterator for outputing the deque on
   //console window
   ostream_iterator<int> out(cout," ");
   //add items to the front of the deque dq
   dq.push_front(12);
   dq.push_front(101);
   dq.push_front(11);
   //add items to the back of the deque dq
   dq.push_back(121);
   dq.push_back(100);
   dq.push_back(111);
   //the items of the deque dq using iterator
   deque<int>::iterator it=dq.begin();
   cout<<"Before removing items: ";  
   while(it!=dq.end()){
               cout<<*it<<" ";
               it++;
               }
   cout<<"\n";
   //delete an item from the front of deque dq
   dq.pop_front();
   //delete an item from the back of deque dq
   dq.pop_back();
   cout<<"After removing items: ";
   copy(dq.begin(),dq.end(),out);
   cout<<"\n";
   //reverse items of deque  
   reverse(dq.begin(),dq.end());
   cout<<"After reversing items: ";
   copy(dq.begin(),dq.end(),out);
   cout<<"\n";
   //sort the deque in ascending order
   sort(dq.begin(),dq.end());
   cout<<"After sorting items in ascending: ";
   copy(dq.begin(),dq.end(),out);
   cout<<"\n";
   //sort the deque in descending order
   //by specifying a predicate function com
   sort(dq.begin(),dq.end(),com);
   cout<<"After sorting items in descending: ";
   copy(dq.begin(),dq.end(),out);
   cout<<"\n";
   //show min item
   cout<<"Min item:"<<*min_element(dq.begin(),dq.end())<<endl;
   //show max item
   cout<<"Max item:"<<*max_element(dq.begin(),dq.end())<<endl;
   // locate first occurrence of 100 in deque      
   deque< int >::iterator fit;      
   fit=find(dq.begin(), dq.end(), 100 );
   if (fit!= dq.end() ) // found 100
     cout << "Found 100\n";
   else // 100 not found
     cout << "100 not found\n";
  
   //remove all items of the deque dq
   dq.clear();

   system("PAUSE");
   return EXIT_SUCCESS;
}


Using-deque-class

Posted by: Dara | post date: 02-02-2014 | Subject: C++ Programming Language

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.