C++ map function

Maps are part of the C++ STL (Standard Template Library). Maps are the associative containers that store sorted key-value pair, in which each key is unique and it can be inserted or deleted but cannot be altered. Values associated with keys can be changed.

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

KeysValues
101Nikita
102Robin
103Deep
104John

Syntax

Parameter

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

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

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 map

Maps can easily be created using the following statement:

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

Example 1

Output:

Employees[104]=Deep

Map size: 5

Natural Order:
101: Nikita
102: Aman
103: Dolly
104: Deep
105: John

Reverse Order:
105: John
104: Deep
103: Dolly
102: Aman
101: Nikita

Member Functions

Below is the list of all member functions of map:

Constructor/Destructor

FunctionsDescription
constructorsConstruct map
destructorsMap destructor
operator=Copy elements of the map to another map.

Iterators

FunctionsDescription
beginReturns an iterator pointing to the first element in the map.
cbeginReturns a const iterator pointing to the first element in the map.
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
emptyReturns true if map is empty.
sizeReturns the number of elements in the map.
max_sizeReturns the maximum size of the map.

Element Access

FunctionsDescription
operator[]Retrieve the element with given key.
atRetrieve the element with given key.

Modifiers

FunctionsDescription
insertInsert element in the map.
eraseErase elements from the map.
swapExchange the content of the map.
clearDelete all the elements of the map.
emplaceConstruct and insert the new elements into the map.
emplace_hintConstruct and insert new elements into the map 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_rangeReturns the range of elements matches with given key.

Allocator

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

Non-Member Overloaded Functions

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

Next TopicC++ Map