=====Map Constructors & Destructors=====
Syntax:
#include
The default constructor takes no arguments, creates a new instance of that map,
and runs in [[/complexity|constant time]]. The default copy constructor runs in [[/complexity|linear time]] and
can be used to create a new map that is a copy of the given map m.
You can also create a map that will contain a copy of the elements between
start and end, or specify a comparison function cmp.
The default destructor is called when the map should be destroyed.
For example, the following code creates a map that associates a string with an
integer:
struct strCmp {
bool operator()( const char* s1, const char* s2 ) const {
return strcmp( s1, s2 ) < 0;
}
};
...
const char *father = "Homer";
const char *mother = "Marge";
const char *kid1 = "Lisa";
const char *kid2 = "Maggie";
const char *kid3 = "Bart";
map ages;
ages[father] = 38;
ages[mother] = 37;
ages[kid1] = 8;
ages[kid2] = 1;
ages[kid3] = 11;
cout << "Bart is " << ages[kid3] << " years old" << endl;
Related Topics: [[map_Operators]]