list operations on a deque : deque pop « Deque « C++






list operations on a deque

  
#include <string>
#include <deque>
#include <algorithm>  
#include <iostream>      
using namespace std;

void print( deque<string> );

int main()
{
    deque<string> animals;                                                  

    animals.push_back( "A" );                                             
    animals.push_back( "B" );                                               
    animals.push_front( "C" );                                            
    animals.push_front( "D" );                                         

    print(animals);               

    animals.pop_front();                                                    
    animals.pop_back();                                                     

    print(animals);               

    //list operations on a deque:
    animals.erase(find( animals.begin(), animals.end(), "E" ));           
    print(animals);               
    animals.insert( animals.begin(), "F" );                            
    print(animals);               
    int sz = animals.size();      
    animals.resize( 5 );          
    animals[sz] = "G";                               
    animals[sz+1] = "H";   
    animals[sz+2] = "I";        
    print( animals );             
    animals.erase( animals.begin() + 2 );                    
    print( animals );             

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

    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.Remove first and last element