C++ STL Map Container – std::map

In this tutorial you will learn about STL Map container in C++ i.e., std::map and all functions applicable on it.

Map is an associative container. Map satisfies the word “associative”. That means every value in map is associated with a key. All keys are unique. No two mapped values can have same key. The type of key and stored values may differ. All elements follow a strict order. When we insert element automatically stored in its correct position.

C++ STL Map

Iterators that can be applicable on map:

begin(): returns iterator to the beginning.

end(): returns iterator to the end of the map.

rbegin(): returns reverse iterator to reverse beginning.

rend(): returns reverse iterator to reverse end.

cbegin(): Returns constant iterator to the beginning.

cend(): Returns constant iterator to the end.

These iterators we can use in our programs.

First thing we need to include map header file. Which is #include<map>

Let see some functions associated with map:

Inserting element into map:

Whenever an element inserted into map, size of the map increases. Since map contains unique keys when we try to insert an element into map, it always checks whether any element already inserted or not with this same key value.

There are different ways we can insert elements into map.

Method 1: Insert directly by passing element and its corresponding key.

mapName.insert (pair <keydataType, value dataType> ( key, value ));

Method 2: Using iterator. This returns iterator at inserted position.

mapName.insert (iterator,  pair < keyDatatype, value datatype > (key value, data value));

Example program for inserting into map:

Output

0 0
1 10
2 20
3 30
4 40
10 10
99 99

Other modification functions on map:

erase(): We can remove single element or a range of elements. The effect on map is whenever a value deleted from map, its size get reduced.

swap(): swaps elements of map1 to map2 and map2 to map1. Here both map1 and map2 are need not be of same size.

clear(): removes all elements in the map. It results map of size 0.

Example program to show above function:

Output

printing remaining elements after one erase operation
1 11
2 12
3 13
4 14
checking map empty or not
map is empty
map1 elements before swap
0 10
1 11
2 12
3 13
4 14
map2 elements before swap
0 0
1 10
2 20
3 30
4 40
map1 elements after swap
0 0
1 10
2 20
3 30
4 40
map2 elements after swap
0 10
1 11
2 12
3 13
4 14
applying clear operation
map is empty by clear operation

Functions related to capacity:

empty(): returns a Boolean value whether map is empty or not.

size(): returns the size of the map.

max_size(): returns the maximum size a map can have.

Example program to show above functions:

Output

map contains some elements
size of the map is 5
maximum size of the map is 461168601842738790

Comment below if you have queries or found any information incorrect in above tutorial for C++ STL Map container i.e. std::map.

Leave a Reply

Your email address will not be published. Required fields are marked *