Erase first element : vector erase « Vector « C++






Erase first element

  
 

#include <iostream>
using std::cout;
using std::endl;

#include <vector>    // vector class-template definition
#include <algorithm> // copy algorithm
#include <iterator>  // ostream_iterator iterator
#include <stdexcept> // out_of_range exception

int main()
{
   int array[ 6 ] = { 1, 2, 3, 4, 5, 6 };
   std::vector< int > integers( array, array + 6 );
   std::ostream_iterator< int > output( cout, " " );

   integers.push_back( 2 );
   integers.push_back( 3 );
   integers.push_back( 4 );

   cout << "Vector integers contains: ";
   std::copy( integers.begin(), integers.end(), output );

   // erase first element
   integers.erase( integers.begin() );
   cout << "\n\nVector integers after erasing first element: ";
   std::copy( integers.begin(), integers.end(), output );


   return 0;
}

/* 
Vector integers contains: 1 2 3 4 5 6 2 3 4

Vector integers after erasing first element: 2 3 4 5 6 2 3 4 
 */        
    
  








Related examples in the same category

1.Remove(delete) all elements in the vector
2.Use unique_copy to remove duplicate words
3.Removes an element from an unordered vector
4.Removes an element from an ordered vector