|
empty
Syntax:
#include <map> bool empty() const; The empty() function returns true if the map has no elements, false otherwise. For example, the following code uses empty() as the stopping condition on a while loop to 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<const char*, int, strCmp> ages;
ages["Homer"] = 38;
ages["Marge"] = 37;
ages["Lisa"] = 8;
ages["Maggie"] = 1;
ages["Bart"] = 11;
while( !ages.empty() ) {
cout << "Erasing: " << (*ages.begin()).first << ", " << (*ages.begin()).second << endl;
ages.erase( ages.begin() );
}
When run, the above code displays: Erasing: Bart, 11 Erasing: Homer, 38 Erasing: Lisa, 8 Erasing: Maggie, 1 Erasing: Marge, 37 |