erase

Syntax:

    #include <vector>
    iterator erase( iterator loc );
    iterator erase( iterator start, iterator end );

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.

For example:

    // Create a vector, load it with the first ten characters of the alphabet
    vector<char> alphas;
    for( int i=0; i < 10; i++ ) {
      alphas.push_back( i + 65 );
    }
    int size = alphas.size();
    vector<char>::iterator startIterator;
    vector<char>::iterator tempIterator;
    for( int i=0; i < size; i++ ) {
      startIterator = alphas.begin();
      alphas.erase( startIterator );
      // Display the vector
      for( tempIterator = alphas.begin(); tempIterator != alphas.end(); tempIterator++ ) {
        cout << *tempIterator;
      }
      cout << endl;
    }

That code would display the following output:

    BCDEFGHIJ
    CDEFGHIJ
    DEFGHIJ
    EFGHIJ
    FGHIJ
    GHIJ
    HIJ
    IJ
    J

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++ ) {
      alphas.push_back( i + 65 );
    }
    // display the complete vector
    for( int 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 vector
    for( int 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).

#include <iostream>
#include <vector>
#include <iterator>
 
using namespace std;
 
int main()
{
    vector<char> alphas;
    for( int i=0; i < 10; i++ ) {
      alphas.push_back( i + 65 );
    }
 
    vector<char>::iterator iter = alphas.begin();
    while( iter != alphas.end() )
    {
      if (*iter == 'B' || *iter == 'D')
        iter = alphas.erase( iter );
      else
        ++iter;
    }
 
    copy(alphas.begin(), alphas.end(), ostream_iterator<char>(cout, ""));
    cout << endl;
 
}

When run, the above code displays:

    ACEFGHIJ

Related Topics: clear, insert, pop_back