C++ Empty Map

We use the empty map function in C++ which lets us know whether the map container is empty inside or not. This check is verified if the “empty map()” returns true when the map container is not having anything in it or if it returns false if the map container has some stored information in it. The map is known to be an STL container that is identified as a data structure that stores the data in sequential or ordered form, so that we can search for any element in the data structure (that is in the dictionary form). This searches for a specific element in the data easily for the user.


Now, we’ll learn how we can declare the empty map object in C++ and make use of the empty map() function by implementing the different examples. Let’s solve the different examples for the empty map in C++.

Example 1:

In the first example, we will first create a map container. Then, we check the elements in that data structure container using the empty map() function. Create the map data structure by importing the map library from the C++ repository in the following way:

$ # include <iostream>

$ # include <map>

The <iostream> is known to be the header file in the C++ language which contains the functions for the programs like Cout (to display) and Cin (to read or to get the input from the user). The “# include” makes sure that these files like map (that we use for various functions like creating a map and more functions of the map) and iostream are included in the code. Then, we define the map using “std map <char, int> map” and assign the values to the map. Now, in the while loop, we check whether the map is empty or not using the map function. If the map is empty, the code returns to zero. Otherwise, it displays the data in the map.

The while loop in the example checks whether the map is empty or not. If it finds the map to be not empty, the data in the map is displayed as shown in the output.

Example 2:

The second example will demonstrate how we create a map using the pair of string and integer and how we apply the empty map() function to that. Import the “namespace std” and “map” library to use their functions. Then, in the main, we define the map <string, char> with the name of the map as “country” since we are creating a map for the country. The elements of the map consist of three names of the country – Pakistan, India, Afghanistan, etc. Then, we assign them some character value as “char”.

In the example, we assign the Pakistan string which is an element of the map (country) char as “P”, India as “I “, and Afghanistan as “A “. Now, to use the empty map() function, we create another map with the name, Country 1. With this map name, we apply the condition on both maps with names Country and Country 1. Now, we use the “empty()” method and impose the condition. If the map is not empty, the Country map is displayed and the output returns to zero. If the map is empty, the Country1 map is displayed and the output returns to 1. The implementation for the previously mentioned example is shown in the following figure:

The output is now displayed for the Country map as return “zero” if the map is not empty and the Country1 map with the return value “1” when the map is empty.

Example 3:

The next example will solve a case in which we first create an empty map and then check whether the map is empty or not. After the verification, insert some elements in the empty map that we already created in the example. Then, check again using the “empty map()” function if the map is still empty or not. We start the example by importing the libraries from the C++ repositories. The libraries to be imported for the example are as follows:

$ # include <iostream>

$ # include <map>

$ # include <utility>

The C++ has the utility library that has the utilities for the unrelated domains. It is known to be the header file of the C++. After importing the required libraries, we now create an empty map with the name “numbers_map” using the “std:: map < int, int >”. The int represents the data type integers for the map elements. Now, we apply a condition on this created map that checks/confirms, if the map is empty or not.

Then, we insert some elements in the map by calling the built-in function of C++ – the “map_name .emplace(int, int)” and the “map_name .insert(int, int)”. Now, after the insertion of the numbers to the empty map, we check if the map is still empty or not by calling again the “map_name. empty()” method.

In the code, we implemented the  previously defined example in a way that it will first display the “map_numbers. empty()” as “true” if the map is empty. Or else, after inserting the elements to the map, it displays the map_numbers. empty() as “false” after we inserted more numbers into the map. The output of the code depicts the return of the Boolean variables true and false for both empty map and non-empty map after the insertion of elements, respectively.


We discussed the empty map function C ++ in this article. We learned how we can create the map using the various methods of insertions for the different data types. We also discussed how we can import the basic libraries for the implementation of code with a brief explanation of the definition of those libraries. Then, we used the empty map() function for different examples where we first created an empty map and checked the value of the map. Then, we inserted some elements in the map and then cross-checked whether the map is still empty or not.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.