map::empty() function with Example in C++ STL

C++ STL map::empty() function with example: Here, we are going to learn about the map::empty() function in C++ STL, that is used to check whether a map is empty or not?
Submitted by Vivek Kothari, on December 03, 2018

C++ STL map::empty()

It is built-in function in C++ STL and used to check whether the map container is empty or not i.e whether its size is 0 or not?

Syntax:

    myMap.empty()

Where, myMap is the object of class map.

Parameters: None - It does not accept any parameters.

Return value: It returns True, if map is empty and returns False, otherwise.

Example:

#include <iostream> 
#include <map> 
using namespace std; 

int main() {
	// Example of Non Empty map
	map<char, string> myMap; 

	myMap['i'] = "include"; 
	myMap['h'] = "help"; 

	if (myMap.empty()) { 
		cout << "myMap is Empty !!"; 
	} 
	else { 
		cout << "myMap contains elements , Not Empty!!"; 
	} 

	cout<<endl<<endl;

	// Example of Empty map
	map<char, int> empMap; 

	if (empMap.empty()) { 
		cout << "empMap is Empty !!"; 
	} 
	else { 
		cout << "empMap contains elements , Not Empty!!"; 
	} 

	return 0;     
} 

Output

myMap contains elements , Not Empty!!

empMap is Empty !!




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.