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 !!

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT


Top MCQs

Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.