map<KeyType, ValueType>

The STL map is one of the most common and useful STL container classes. map represents an arbitrary mapping of key-value pairs without duplicates and is useful for modeling relations between elements of one type and another. For a similar container where keys need not be unique, consider the multimap. For a similar container storing keys without values, use set.

Unlike other STL container classes, map iterators act as pointers to objects of type pair<const KeyType, ValueType>, meaning that when iterating over a map, access the first field to get the key and the second field to get the value.

By default, the map uses operator < to order the keys behind the scenes. To use an alternative comparison function, parametrize the map as map<KeyType, ValueType, CompareFunctorType>.

Common map<KeyType, ValueType> Member Functions
Constructor: map<KeyType, ValueType> ()
Constructs a new empty map.
map<string, int> myMap; // Constructs a new, empty map.

Constructs a new map. This is the default constructor.

ValueType& operator [](const KeyType &key)
Return associated value
myMap["C++ is awesome!"] = true; // Associates "C++ is awesome!" with value true.

Returns a reference to the key's associated value in the map. If the key doesn't exist, a new key/value pair will automatically be inserted. Note that this function is not marked const and cannot be used with const maps. To look up elements in a const map, use find or count instead.

Note that while it's syntactically more convenient to use the brackets to insert elements into a map, for complex ValueType objects, it is considerably more efficient to use insert instead.

pair<iterator, bool> insert(const pair<const KeyType, ValueType> &elem)
void insert(InputIterator start, InputIterator stop)
Insert key/value pairs
myMap.insert(make_pair(string("C++ is awesome!", true))); // Insert key "C++ is awesome!" with value true
myMap.insert(myOtherContainer.begin(), myOtherContainer.end()); // Insert key-value pairs from another iterator range.

Inserts new elements into the map. insert is perhaps the most complicated STL function you will routinely encounter in practice. The first version of insert inserts a new key/value pair into the map, accepting a pair<const KeyType, ValueType> as a parameter. You can create this pair explicitly, or can use the utility function make_pair instead. insert returns a pair<iterator, bool>, where the first field of the pair is an iterator to the element and the second field is a bool representing whether the element was inserted. If the key already existed, this value will be false; if the key/value pair was created for the first time, it will be true. Make sure to check this value if you suspect a key/value pair may already exist and want to overwrite it, or use the operator [] function instead.

The second version of this function inserts a range of elements into the map, removing duplicates. The elements in the range [start, stop) must be of type pair<KeyType, ValueType>.

iterator find(const KeyType &key)
const_iterator find(const KeyType &key) const
Return iterator to element
if(myMap.find("C++ is awesome!") == myMap.end()) // "C++ is awesome!" is not a key

Returns an iterator to the key/value pair with the specified key, if it exists, and the result of the map's end function otherwise.

size_type count(const KeyType &key) const
Determine membership
if(myMap.count("C++ is awesome!")) // "C++ is awesome!" is a key

Returns 1 if the specified key is contained in the map and 0 otherwise. count is a useful function, but in practice it's far more common to see find.

size_type erase(const KeyType &key)
void erase(iterator elem)
void erase(iterator start, iterator stop)
Remove elements from the map
myMap.erase("C++ is not awesome!"); // Remove all entries with key "C++ is not awesome!"
myMap.erase(myItr); // Erase the element pointed at by myItr
myMap.erase(myMap.begin(), myMap.end()); // Remove all elements from the container

Removes elements from the map. The first version removes any elements with the specified key from the map, returning 1 if the element existed and was removed and 0 otherwise. The second version removes from the map the element pointed at by the specified iterator. The third version removes all elements in the range [start, stop) from the map.

bool empty() const
Is container empty?
if(myMap.empty()) { ... }

Returns whether the map is empty. It's considered good practice to call empty to determine whether the map is empty rather than checking whether size() returns zero.

void clear()
Empty container
myMap.clear();

Removes all elements from the map.

size_type size() const
Get container size
int numElems = myMap.size();

Returns the number of elements stored in the map.

iterator begin()
const_iterator begin() const
Get start iterator.
for(map<string, int>::iterator itr = myMap.begin(); /* ... */)

Returns an iterator to the first element in the map. Elements in a map are stored in sorted order by key, so the elements returned by iterating over the map will be sorted. Remember that map iterators act as pointers to pair<const KeyType, ValueType>.

iterator end()
const_iterator end() const
Get stop iterator.
for(/* ... */ itr != myMap.end(); /* ... */)

Returns an iterator to the element one past the end of the map. This iterator does not point to a valid element, so do not dereference it.

Other map<KeyType, ValueType> Member Functions
Constructor: map<KeyType, ValueType> (InputIterator begin, InputIterator end)
Constructs a pre-initialized map
map<string, int> myMap(otherContainer.begin(), otherContainer.end()); // Copy elements from the range otherContainer.begin(), otherContainer.end() into this map

Constructs a new map whose elements are equal to the elements defined in the range [begin, end). These iterators need not come from another map, but they must be of the form pair<KeyType, ValueType>.

iterator lower_bound(const KeyType &key)
const_iterator lower_bound(const KeyType &key) const
Get first element greater than or equal to
map<string, int>::iterator itr = myMap.lower_bound("b"); // Get iterator to first key in the map alphabetically following "b"

Returns an iterator to the first key/value pair whose key compares greater than or equal to the parameter. For example, lower_bound(14) would return an iterator to the first element of a map<int, string> whose key was strictly greater than or equal to 14.

iterator upper_bound(const KeyType &key)
const_iterator upper_bound(const KeyType &key) const
Get first element strictly greater than
map<string, int>::iterator itr = myMap.upper_bound("b"); // Get iterator to one past the last element less than or equal to "b"

Returns an iterator to the first key/value pair whose key compares strictly greater than the parameter.

pair<iterator, iterator> equal_range(const KeyType &key)
pair<const_iterator, const_iterator> equal_range(const KeyType &key) const
Get two iterators defining the range of a single element
pair<map<string, int>::iterator, map<string, int>::iterator> equal = myMap.equal_range("C++!");

Returns an pair of iterators defining the range containing the single element specified as a parameter. If the element does not exist, the two iterators will be equal.

size_type max_size() const
Get maximum container size
map<string, int>::size_type maxElems = myMap.max_size()

Returns the maximum number of elements the map can hold, which is usually the number of unique memory addresses divided by the size of the elements stored in the container.

BinaryFunction key_comp() const
Get key comparison function
if(myMap.keyComp()(myKey1, myKey2)) // Key myKey1 compares less than myKey2

Returns the map's internal comparison function used to compare two keys. You can use this function to compare two keys in the same way as the map.

BinaryFunction value_comp() const
Get key/value pair comparison function
if(myMap.keyComp()(*itr1, *itr2)) // Element referenced by first iterator comes before element referenced by second iterator

Returns the map's internal comparison function used to compare two key/value pairs. That is, given two pair<const KeyType, ValueType>s, you can use value_comp to determine which one precedes the other in the map's internal ordering.

reverse_iterator begin()
const_reverse_iterator begin() const
Get start reverse iterator
for(map<string, int>::reverse_iterator itr = myMap.rbegin(); /* ... */)

Returns a reverse_iterator to the last element of the map. reverse_iterators are like regular iterators except that they traverse in the opposite direction. Check for the end-of-container condition of reverse_iterators with rend.

reverse_iterator end()
const_reverse_iterator end() const
Get stop reverse iterator.
for(/* ... */ itr != myMap.rend(); /* ... */)

Returns a reverse_iterator to the element one element before the start of the array. Like end, the iterator returned by rend does not actually reference a real element of the map and you should take care not to actually dereference it.

void swap(map<KeyType, ValueType> &other)
Exchange two mapss
myMap.swap(myOtherMap); // Exchanges the two maps

Exchanges the contents of this map and another map.