Deque: push, pop, sort, find, begin, end, insert : Deque « Data Structure « C++






Deque: push, pop, sort, find, begin, end, insert

Deque: push, pop, sort, find, begin, end, insert
#include <string>
#include <deque>
#include <iostream>
#include <algorithm>
using namespace std;

void print( deque<string> );

int main()
{
    deque<string> dequeObject;                                                  

    dequeObject.push_back( "yak" );                                             
    dequeObject.push_back( "zebra" );                                               
    dequeObject.push_front( "cat" );                                            
    dequeObject.push_front( "canary" );                                         

    print(dequeObject);

    dequeObject.pop_front();                                                    
    dequeObject.pop_back();                                                     

    print(dequeObject);
    //list operations on a deque:
    dequeObject.erase(find( dequeObject.begin(), dequeObject.end(), "cat" ));           
    print(dequeObject);
    dequeObject.insert( dequeObject.begin(), "canary" );                            
    print(dequeObject);
    int sz = dequeObject.size();      
    dequeObject.resize( 5 );          
    dequeObject[sz] = "fox";          
    dequeObject[sz+1] = "elephant";   
    dequeObject[sz+2] = "cat";        
    print( dequeObject );             
    dequeObject.erase( dequeObject.begin() + 2 ); 
    print( dequeObject );             

    //sorting a deque:
    sort( dequeObject.begin(), dequeObject.end() );                                 
    print( dequeObject );           

    return 0;
}

void print( deque<string> d ) {
    typedef deque<string>::const_iterator CI;                 
    cout << "The number of items in the deque:" << d.size() << endl; 
    for ( CI iter = d.begin(); iter != d.end(); iter++ )
         cout << *iter << " ";
    cout << endl << endl;
}

           
       








Related examples in the same category

1.One way to reverse-copy a deque.One way to reverse-copy a deque.
2.Use reverse iterators.Use reverse iterators.
3.Reverse iterators and copy.Reverse iterators and copy.
4.Assigning deque objects.Assigning deque objects.
5.Demonstrate swap() for dequeDemonstrate swap() for deque
6.Demonstrate resize() for dequeDemonstrate resize() for deque
7.Iterator values may change.Iterator values may change.
8.Demonstrate raw storage iteratorsDemonstrate raw storage iterators
9.Demonstrate a deque.Demonstrate a deque.