erase

Syntax:

    #include <set>
    void erase( iterator pos );
    void erase( iterator start, iterator end );
    size_type erase( const key_type& key );

The erase() function either erases the element at pos, erases the elements between start and end, or erases all elements that have the value of key.

For example, the following code uses erase() to erase all matching elements and a single explicit element:

    multiset<int> ms;
    multiset<int>::iterator iter;
    int i;
 
    for (i = 1; i < 5; i++) {
        ms.insert(i);
        ms.insert(i*i);
        ms.insert(i-1);
    }
 
    cout << "ms is:" ;
    for (iter = ms.begin(); iter != ms.end(); iter++)
        cout << " " << *iter;
    cout << "." << endl;
 
    ms.erase( 1 );
 
    iter = ms.find( 3 );
    if ( iter != ms.end() ) {
        ms.erase( iter );
    }
 
    cout << "ms is now:" ;
    for (iter = ms.begin(); iter != ms.end(); iter++)
        cout << " " << *iter;
    cout << "." << endl;

The above code produces the following output:

ms is: 0 1 1 1 2 2 3 3 4 4 9 16.
ms is now: 0 2 2 3 4 4 9 16.

Note that all three 1s were removed, but only a single 3 was removed.