C++ Standard Library C++ STL Library

C++ - <map>



The C++ <map> header file defines the map and multimap container classes:

map class

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

typedef pair<const Key, T> value_type;

Each key in a map is unique which can be inserted or deleted in the map but can not be altered. Values associated with keys can be changed. The value in a map can be directly accessed by specifying corresponding key using the operator [].

Maps 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 map; 

Parameters

Key Type of the key.
T Type of the mapped value.
Compare A binary predicate that takes two keys of map 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++ map - Member Functions

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

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

Capacity

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

Element Access

FunctionsDescription
at() Access an element of the map.
operator[]() Access an element of the map.

Iterators

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

Modifiers

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

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 map::end.
count() Returns the number of occurrences of a key in the map container.
lower_bound() Returns an iterator pointing to the first element in the map container whose key is not considered to go before specified value.
upper_bound() Returns an iterator pointing to the first element in the map container whose key is considered to go after specified value.
equal_range() Returns the range of elements in the map container with a key that matches with the given value.

Allocator

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

C++ map - Non-member Functions

FunctionsDescription
swap() Exchanges elements between two maps.

multimap class

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 - Member Functions

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

FunctionsDescription
multimap() Construct a multimap object.
~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.

Allocator

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

C++ multimap - Non-member Functions

FunctionsDescription
swap() Exchanges elements between two multimaps.