C++ map empty() Function

C++ map empty() function is used to check whether the map container is empty or not. It returns true, if the map container is empty (size is 0) otherwise false.

Syntax

Parameter

None

Return value

It returns true, if the map container is empty (size is 0) otherwise false.

Example 1

Let's see a simple example to check if a map contains any element or not.

Output:

 Initially, numbers.empty(): 1

 After adding elements, numbers.empty(): 0

In the above example, initially size of map is 0 hence, empty() function returns 1(true) and after adding elements it returns 0(false).

Example 2

Let's see a simple example to check whether map is empty or not.

Output:

Map is empty
Map is not empty

In the above example, if condition statement is used. If map is empty it will return map is empty after adding elements it will return map is not empty.

Example 3

Let's see a simple example.

Output:

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

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

Example 4

Let's see a simple example.

Output:

Map is empty. Please insert content! 
 
Enter three sets of name and number: 
Nikita 555555
Nidhi  111111
Deep  333333

List of telephone numbers: 
Deep 333333 
Nidhi 111111 
Nikita 555555

In the above example, the program first creates phone map interactively with three names. Then, it checks the map is empty or not. If map is empty then it displays a message otherwise, it displays all the names and their telephone numbers available in the map.

Next TopicC++ Map



Latest Courses