Facebook Page Twitter Page LinkedIn Page
× C++ Standard Library C++ STL Library


C++ Multimap

A multimap is an associative container that stores data sequence in key-value pair which is sorted by key. Unlike map, in multimap two mapped values can have the same key. The data type of key and value may differ and can be represented as:

typedef pair<const Key, T> value_type;

In a multimap, Keys can be inserted or deleted but can not be altered. Values associated with keys can be changed.

Multimaps are typically implemented as Binary Search Tree.

Syntax

template < class Key,                                     // key_type
           class T,                                       // mapped_type
           class Compare = less<Key>,                     // key_compare
           class Alloc = allocator<pair<const Key,T>>     // allocator_type
           > class multimap; 

Parameters

Key Type of the key.
T Type of the mapped value.
Compare A binary predicate that takes two keys of multimap as arguments and returns a bool. Keys are sorted by using this function.
Alloc Type of the allocator object used to define the storage allocation model, default:allocator.

Member Types

Member typesDefinition
key_type Key (First template parameter)
mapped_type T (Second template parameter)
key_compare Compare (Third template parameter), default: less<key_type>
allocator_type Alloc (Fourth template parameter), default: allocator<value_type>
value_type pair<const key_type,mapped_type>
value_compare Nested function class to compare elements
reference value_type&
const_reference const value_type&
pointer Alloc::pointer, default: value_type*
const_pointer Alloc::const_pointer, default: value_type*
iterator a bidirectional iterator to value_type
const_iterator a bidirectional iterator to const value_type
reverse_iterator reverse_iterator <iterator>
const_reverse_iterator reverse_iterator <const_iterator>
difference_type ptrdiff_t
size_type size_t

C++ - Multimap Functions

The C++ multimap container has a number of member functions which are listed below:

FunctionsDescription
~multimap() Destroys container by deallocating container memory.
operator=() Assign content to a multimap.

Capacity

FunctionsDescription
empty() Checks whether the multimap is empty or not.
size() Returns the size of the multimap.
max_size() Returns the maximum size of the multimap.

Iterators

FunctionsDescription
begin() Returns iterator pointing to the first element of the multimap.
end() Returns iterator pointing to the past-the-last element of the multimap.
rbegin() Returns reverse iterator to the last element of the multimap.
rend() Returns reverse iterator to the element preceding the first element of the multimap.
cbegin() Returns const_iterator pointing to the first element of the multimap.
cend() Returns const_iterator pointing to the past-the-last element of the multimap.
crbegin() Returns const_reverse_iterator to the last element of the multimap.
crend() Returns const_reverse_iterator to the element preceding the first element of the multimap.

Modifiers

FunctionsDescription
clear() Clears all elements of the multimap.
erase() Deletes either a single element or range of elements from a multimap.
insert() Insert elements in the multimap.
swap() Exchanges elements between two multimaps.
emplace() Constructs and inserts a new element in the multimap.
emplace_hint() Constructs and inserts a new element with hint in the multimap.

Observers

FunctionsDescription
key_comp() Returns a copy of the comparison object used by the container.
value_comp() Returns a copy of the comparison object used by the container.

Operations

FunctionsDescription
find() Searches the container for a key and returns the iterator to it if found, else returns the iterator to multimap::end.
count() Returns the number of occurrences of a key in the multimap container.
lower_bound() Returns an iterator pointing to the first element in the multimap container whose key is not considered to go before specified value.
upper_bound() Returns an iterator pointing to the first element in the multimap container whose key is considered to go after specified value.
equal_range() Returns the range of elements in the multimap container with a key that matches with the given value.

Non-member function overloads

FunctionsDescription
swap() Exchanges elements between two multimaps.

Allocator

FunctionsDescription
get_allocator() Return a copy of allocator object associated with the multimap.