This documentation is archived and is not being maintained.

vector::erase 

Removes an element or a range of elements in a vector from specified positions.


iterator erase(
   iterator _Where
);
iterator erase(
   iterator _First,
   iterator _Last
);

Parameters

_Where

Position of the element to be removed from the vector.

_First

Position of the first element removed from the vector.

_Last

Position just beyond the last element removed from the vector.

An iterator that designates the first element remaining beyond any elements removed, or a pointer to the end of the vector if no such element exists.

// vector_erase.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>

int main( )
{
   using namespace std;   
   vector <int> v1;
   vector <int>::iterator Iter;
   
   v1.push_back( 10 );
   v1.push_back( 20 );
   v1.push_back( 30 );
   v1.push_back( 40 );
   v1.push_back( 50 );

   cout << "v1 =" ;
   for ( Iter = v1.begin( ) ; Iter != v1.end( ) ; Iter++ )
      cout << " " << *Iter;
   cout << endl;

   v1.erase( v1.begin( ) );
   cout << "v1 =";
   for ( Iter = v1.begin( ) ; Iter != v1.end( ) ; Iter++ )
      cout << " " << *Iter;
   cout << endl;

   v1.erase( v1.begin( ) + 1, v1.begin( ) + 3 );
   cout << "v1 =";
   for ( Iter = v1.begin( ) ; Iter != v1.end( ) ; Iter++ )
      cout << " " << *Iter;
   cout << endl;
}

Output

v1 = 10 20 30 40 50
v1 = 20 30 40 50
v1 = 20 50

Header: <vector>

Show: