C++ map size() Function

C++ map size() function is used to find the number of elements present in the map container.

Syntax

Member type size_type is an unsigned integral type.

Parameter

None

Return value

It returns the number of elements present in the map.

Example 1

Let's see a simple example to calculate the size of the map.

Output:

 num map contains 4 elements.

In the above example, map num contains 4 elements. Therefore ,size() function returns 4 elements.

Example 2

Let's see a simple example to calculate initial size of map and size of map after adding elements.

Output:

Initial size of map = 0
Size of map after inserting elements = 5

In the above example, first map is empty. Hence, size() function will return 0 and after inserting 5 elements it will return 5.

Example 3

Let's see a simple example.

Output:

x => 100
y => 200
z => 300

In the above example, It simply use the size() function in while loop and prints the elements of map until the size of map.

Example 4

Let's see a simple example.

Output:

Enter three sets of name and number: 
Nikita 1001
Deep   2001
Aashi  3001

Size of phone map is:3
List of telephone numbers: 
Aashi 3001 
Deep 2001 
Nikita 1001

In the above example, the program first creates phone map interactively with three names. Then, it displays the total size of phone map and all the names and their telephone numbers available in the map.

Next TopicC++ Map