=====erase=====
Syntax:
#include
The ''erase'' method either:
- erases the element at ''pos'',
- erases the elements from ''start'' to ''end'' (but not including ''end''),
- or erases all elements that have the value of ''key''.
Note that the first version of ''erase'' invalidates the iterator ''pos''.
For example, the following code uses ''erase'' in a while loop to incrementally clear a map and display its contents in order:
struct strCmp {
bool operator()( const char* s1, const char* s2 ) const {
return strcmp( s1, s2 ) < 0;
}
};
...
map ages;
ages["Homer"] = 38;
ages["Marge"] = 37;
ages["Lisa"] = 8;
ages["Maggie"] = 1;
ages["Bart"] = 10;
while( !ages.empty() ) {
cout << "Erasing: " << ages.begin()->first << ", " << ages.begin()->second << endl;
ages.erase( ages.begin() );
}
When run, the above code displays:
Erasing: Bart, 10
Erasing: Homer, 38
Erasing: Lisa, 8
Erasing: Maggie, 1
Erasing: Marge, 37
Related Topics: [[begin]], [[clear]], [[empty]], [[size]]