Map operators
Syntax:
#include <map> TYPE& operator[]( const key_type& key ); map operator=(const map& c2); bool operator==(const map& c1, const map& c2); bool operator!=(const map& c1, const map& c2); bool operator<(const map& c1, const map& c2); bool operator>(const map& c1, const map& c2); bool operator<=(const map& c1, const map& c2); bool operator>=(const map& c1, const map& c2); Maps can be compared and assigned with the standard comparison operators: ==, !=, <=, >=, <, >, and =. Individual elements of a map can be examined with the [] operator. Performing a comparison or assigning one map to another takes linear time. Two maps are equal if: - Their size is the same, and
- Each member in location
*i*in one map is equal to the the member in location*i*in the other map.
Comparisons among maps are done lexicographically. For example, the following code defines a map between strings and integers and loads values into the map using the [] operator: 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; cout << "Bart is " << ages["Bart"] << " years old" << endl; cout << "In alphabetical order: " << endl; for( map<const char*, int, strCmp>::iterator iter = ages.begin(); iter != ages.end(); iter++ ) { cout << (*iter).first << " is " << (*iter).second << " years old" << endl; } When run, the above code displays this output: Bart is 11 years old In alphabetical order: Bart is 11 years old Homer is 38 years old Lisa is 8 years old Maggie is 1 years old Marge is 37 years old |