Export (0) Print
Expand All

vector::erase

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

iterator erase(
   const_iterator_Where
);
iterator erase(
   const_iterator _First,
   const_iterator_Last
);

Parameter

Description

_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;
}
v1 = 10 20 30 40 50
v1 = 20 30 40 50
v1 = 20 50

Header: <vector>

Namespace: std

Community Additions

ADD
Show:
© 2014 Microsoft