The erase() function either deletes the element at location loc, or deletes the
elements between start and end (including start but not including end). The
return value is the element after the last element erased.

The first version of erase (the version that deletes a single element at
location loc) runs in constant time for lists and linear time for vectors,
dequeues, and strings. The multiple-element version of erase always takes
linear time.

In the next example, erase() is called with two iterators to delete a range of
elements from a vector:

// create a vector, load it with the first ten characters of the alphabet
vector<char> alphas;for(int i=0; i <10; i++){staticconstchar letters[]="ABCDEFGHIJ";
alphas.push_back( letters[i]);}// display the complete vectorfor( vector<char>::size_type i =0; i < alphas.size(); i++){cout<< alphas[i];}cout<< endl;// use erase to remove all but the first two and last three elements// of the vector
alphas.erase( alphas.begin()+2, alphas.end()-3 );// display the modified vectorfor( vector<char>::size_type i =0; i < alphas.size(); i++){cout<< alphas[i];}cout<< endl;

When run, the above code displays:

ABCDEFGHIJ
ABHIJ

With all container types you have to be careful when inserting or erasing elements, since it may lead to invalid iterators.

Here is an example that works for std::vector. Especially, vector::erase() invalidates all iterators (and pointers) following the element to be erased. The example erases some elements depending on a condition (it will erase the letters B and D).