A map is a data structure in which each element is made up of two parts: a key and a value. The key field is a unique identification value that is used to order and access the elements of the structure. The value field is the actual data that is stored for the particular key.
map< key_type, value_type > mapName; map< key_type, value_type, compare_type > mapName; map< key_type, value_type > mapName( otherMap ); map< key_type, value_type, compare_type > mapName( otherMap );
The default ordering (or comparison) for a map is less than but that can be changed when a map is created.
As with the other containers, the map class contains the common methods such as:
iterator begin();
iterator end();
void clear();
void erase( iterator );
void erase( start_iterator, end_iterator );
bool empty() const;
int size() const;
Some of the other methods in the class include:
| Method | Description |
| int count( const key_type& key_value ) const; | Returns the number of elements in the map with the key key_value. (0 or 1) |
| int erase( const key_type& key_value ); | Removes the element with the key key_value and returns the number that were removed. (0 or 1) |
| iterator find( const key_type& key_value); | Returns an iterator referencing the element with key key_value. |
| pair<key_type, value_type> insert( const pair<key_type, value_type>(key_value, value)); | Inserts the pair (key_value, value) into the map BEFORE the element referenced by iterator i. |
| iterator insert( iterator i, const pair<key_type, value_type>(key_value, value)); | Inserts the pair (key_value, value) into the map BEFORE the element referenced by iterator i. |
| void insert( iterator first, iterator last ); | Insert pairs from the range indicated by first and last. |
| compare_type key_comp() const; | Returns the comparison function for keys. |
| value_type operator[]( const key_type& key_value ); | Return the element with the key key_value if it is in the map; otherwise insert it in the map. |
The map container allows for all of the standard iterator operations and methods. An iterator can also be used to access the key and value fields by using the members: first and second.
The first member references the key value for a map element, while the second member references the value field for a map element.
map<key_type, value_type>::iterator mapIter; (*mapIter).first OR mapIter->first //both will give the key value (*mapIter).second OR mapIter->second //both will give the value field for the element (*mapIter) //gives the element value, which is of type pair<key_type, value_type>
map<string, int> wordcounts;
string s;
while( cin >> s && s != "end" )
wordcounts[s]++;
---------------------------------------------------------------------------
map<char, int> myMap;
char c;
// insert elements using insert function
myMap.insert(pair<char, int>('a', 1));
myMap.insert(pair<char, int>('b', 2));
myMap.insert(pair<char, int>('c', 3));
myMap.insert(pair<char, int>('d', 4));
myMap.insert(pair<char, int>('e', 5));
// erase the first element using the erase function
map<char, int>::iterator iter = myMap.begin();
myMap.erase(iter);
// output the size of the map
cout << "Size of myMap: " << myMap.size() << endl;
cout << "Enter a key to search for: ";
cin >> c;
// find will return an iterator to the matching element if it is found
// or to the end of the map if the key is not found
iter = myMap.find( c );
if( iter != myMap.end() )
cout << "Value is: " << iter->second << endl;
else
cout << "Key is not in myMap" << endl;
// clear the entries in the map
myMap.clear();
---------------------------------------------------------------------------
map<string, int> data;
map<string, int>::iterator it
data["Bob"] = 10;
data["Marty"] = 15;
data["Melvin"] = 34;
data["Robert"] = 22;
// Iterate over the map and print out all key/value pairs.
for( it = data.begin(); it != data.end(); it++ )
{
cout << "Who: " << it->first;
cout << " Score): " << it->second << endl;
}
A multimap is a map that allows for duplicate keys. It uses the same member functions as the map contain with the exception of one of the insert methods
pair<key_type, value_type> insert( const pair<key_type, value_type>(key_value, value));
is not supported for a multimap. It is replaced by:
iterator insert( const pair<key_type, value_type>(key_value, value));
which returns an iterator referencing the position where the pair(key_value, value) was inserted.
The other method that is not supported is the operator[] (subscript operator) method. In order to modify an element in a multimap, the best approach is to find the element using the find() method and the second member.