MAP and MULTIMAP.                       

The map class supports an associative container in which unique keys are mapped with values.  A key is simply a name associated with a value. Once a value has been stored, it can be retrieved by its key.  The power of a map is that a value can be looked up given its key.  For example, you could define a map that uses a person’s name as its key and stores that person’s telephone number as its value.

 Multimap is similar to map except that it allows duplicate keys. Thus, one key can be used for more than one value. 

To use map or multimap class include <map>.

The template specification for map:

template <class Key, class T, class Comp=less<Key>, class Allocator=allocator <T> > class map 

where Key is the data type of keys, T is  the data type of the value being stored (mapped), Comp is the function that compares two keys, and Allocator specifies the allocator which defaults to the standard allocator. 

map has following constructors:

explicit map (const Comp &cmpfn=Comp(),                                             const Allocator &a=Allocator() );

Constructs an empty map.

map (const map <Key, T, Comp, Allocator> &ob);

map's copy constructor.

template <class InIter> map (InIter start, InIter end,                                              const Comp &cmpfn=Comp(),                                              const Allocator &a=Allocator() );

Constructs a map that contains the elements in the range specified by start and end.

The map class supports bi-directional iterators. Thus, the container can be accessed through an iterator in both forward and reverse direction.  Random access operations are not supported.

The map class overloads the assignment operator =. It also defines comparison operators:

==, <, <=, !=, >, >=    

Key/value pairs are stored in a map as objects of type pair. A pair contains two fields:

The data type of the pair must match those of the map into which the pair are being inserted.

The iterator type defined by map points to objects of type pair<Key, T>. To obtain an iterator for map:

map <Key, T>::iterator i;    

 Some of the most commonly used member functions: 

Member

Description

iterator begin ();

Returns an iterator to the first element in the map.

iterator end ();

Returns an iterator to the end of the map.

bool empty ();

Returns true if the map contains no elements.

iterator erase (iterator i)

Removes the element pointed to by i. Returns an iterator to the element after the one removed.

iterator erase (iterator start, iterator end);

Removes the elements in range start to end. Returns an iterator to the element after the one removed.

size_type erase (const key_type &k);

Removes from the map elements that have keys with value k.

iterator find (const key_type &k);

Returns an iterator to the specified key. If the key is not found, then an iterator to the end of the map is returned.

iterator insert (iterator i, const T &val);

Inserts val at or after the element specified by i.  An iterator to the element is returned.

template <class InIter>                             void insert (InIter start, InIter end);

Inserts a range of elements defined by start and end immediately before the element specified by i.

pair<iterator, bool>                                  insert (const value_type &val);

Inserts val into the map only if it is not already there. If the element is inserted returns pair<iterator, true>, otherwise, pair<iterator, false> is returned.

reference operator [ ] (const key_type &k);

Returns a reference to the value associated with the key specified by k. If this element does not exist, it is inserted.

size_type size () const;

Returns the number of elements currently in the list.

 Example  

#include<iostream>
#include <map>
#include<string>
using namespace std;

template <class T1,class T2>
void print(map<T1,T2> m) {
    map<T1,T2>::iterator p = m.begin();
    while(p != m.end()) {
        cout<<p->first<<"\t"<<p->second<<"\n";
        p++;
    }
    cout<<endl;
}

main() {
    map<string, string> m;
    m["John"]="555-1234";
    m["Daniel"]="797-3456";
    m["Anna"]="990-8901";
    print(m);
    m.insert(pair<string, string>("Tom", "123-4567"));
    map<string, string>::iterator p = m.find("Tom");
    if (p != m.end())
        cout<<p->first<<"\t"<<p->second<<endl;
    else
        cout<<"Key not in map.\n";
    return 0;
}