Translations of this page?:

Map Constructors & Destructors

Syntax:

    #include <map>
    map();
    map( const map& m );
    map( input_iterator start, input_iterator end );
    map( input_iterator start, input_iterator end, const key_compare& cmp );
    explicit map( const key_compare& cmp );
    ~map();

The default constructor takes no arguments, creates a new instance of that map, and runs in constant time. The default copy constructor runs in 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<const char*, int, strCmp> 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

 
• • • IndexRecent changesRSScc