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






#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








16.13.vector erase
16.13.1.Remove(delete) all elements in the vector
16.13.2.Erase first element
16.13.3.erase the numbers 2 through 5 in v1
16.13.4.insert and erase.
16.13.5.Use unique_copy to remove duplicate words
16.13.6.Erase adjacent duplicates
16.13.7.Erase all value in a vector more than three standard deviations greater than the mean
16.13.8.Erase all value in a vector more than three standard deviations less than the mean with erase() remove_if() and bind2nd