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>.
map.Constructs a new map. This is the default constructor.
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.
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>.
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.
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.
mapRemoves 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.
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.
Removes all elements from the map.
Returns the number of elements stored in the map.
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>.
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.
mapmapConstructs 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>.
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.
Returns an iterator to the first key/value pair whose key compares strictly greater than the parameter.
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.
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.
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.
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.
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.
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.
mapssExchanges the contents of this map and another map.