C++ multimap

Multimaps are part of the C++ STL (Standard Template Library). Multimaps are the associative containers like map that stores sorted key-value pair, but unlike maps which store only unique keys, multimap can have duplicate keys. By default it uses < operator to compare the keys.

For example: A multimap of Employees where employee age is the key and name is the value can be represented as:

KeysValues
23Nikita
28Robin
25Deep
25Aman

Multimap employee has duplicate keys age.

Syntax

Parameter

key: The key data type to be stored in the multimap.

type: The data type of value to be stored in the multimap.

compare: A comparison class that takes two arguments of the same type bool and returns a value. This argument is optional and the binary predicate less<"key"> is the default value.

alloc: Type of the allocator object. This argument is optional and the default value is allocator.

Creating a multimap

Multimaps can easily be created using the following statement:

The above form will use to create a multimap with key of type Key_type and value of type value_type. One important thing is that key of a multimap and corresponding values are always inserted as a pair, you cannot insert only key or just a value in a multimap.

Example

Output:

Size of map m: 4
Elements in m: 
  [India, New Delhi]
  [India, Hyderabad]
  [United Kingdom, London]
  [United States, Washington D.C]

Member Functions

Below is the list of all member functions of multimap:

Constructor/Destructor

FunctionsDescription
constructorConstruct multimap
destructorMultimap destructor
operator=Copy elements of the multimap to another multimap.

Iterators

FunctionsDescription
beginReturns an iterator pointing to the first element in the multimap.
cbeginReturns a const_iterator pointing to the first element in the multimap.
endReturns an iterator pointing to the past-end.
cendReturns a constant iterator pointing to the past-end.
rbeginReturns a reverse iterator pointing to the end.
rendReturns a reverse iterator pointing to the beginning.
crbeginReturns a constant reverse iterator pointing to the end.
crendReturns a constant reverse iterator pointing to the beginning.

Capacity

FunctionsDescription
emptyReturn true if multimap is empty.
sizeReturns the number of elements in the multimap.
max_sizeReturns the maximum size of the multimap.

Modifiers

FunctionsDescription
insertInsert element in the multimap.
eraseErase elements from the multimap.
swapExchange the content of the multimap.
clearDelete all the elements of the multimap.
emplaceConstruct and insert the new elements into the multimap.
emplace_hintConstruct and insert new elements into the multimap by hint.

Observers

FunctionsDescription
key_compReturn a copy of key comparison object.
value_compReturn a copy of value comparison object.

Operations

FunctionsDescription
findSearch for an element with given key.
countGets the number of elements matching with given key.
lower_boundReturns an iterator to lower bound.
upper_boundReturns an iterator to upper bound.
equal_range()Returns the range of elements matches with given key.

Allocator

FunctionsDescription
get_allocatorReturns an allocator object that is used to construct the multimap.

Non-Member Overloaded Functions

FunctionsDescription
operator==Checks whether the two multimaps are equal or not.
operator!=Checks whether the two multimaps are equal or not.
operator<Checks whether the first multimap is less than other or not.
operator<=Checks whether the first multimap is less than or equal to other or not.
operator>Checks whether the first multimap is greater than other or not.
operator>=Checks whether the first multimap is greater than equal to other or not.
swap()Exchanges the element of two multimaps.
Next TopicC++ multimap