map::empty() in C++ STL


In this article we will be discussing the working, syntax and examples of map::empty() function in C++ STL.

What is a Map in C++ STL?

Maps are the associative container, which facilitates to store the elements formed by a combination of key value and mapped value in a specific order. In a map container the data is internally always sorted with the help of its associated keys. The values in the map container are accessed by its unique keys.

What is a map::empty()?

map::empty() function is an inbuilt function in C++ STL, which is defined in  header file. empty() is used to check whether the associated map container is empty or not

This function checks if the size of the container is 0 then returns true, else if there are some values then it returns false.

Syntax

map_name.empty();

Parameters

The function accepts no parameter.

Return value

This function returns true if the map is empty and false if it is not.

Example

Input

std::map<int> mymap;
mymap.insert({‘a’, 10});
mymap.insert({‘b’, 20});
mymap.insert({‘c’, 30});
mymap.empty();

Output

false

Input

std::map<int> mymap;
mymap.empty();

Output

true

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main() {
   map<int, int> TP_1;
   TP_1[1] = 10;
   TP_1[2] = 20;
   TP_1[3] = 30;
   TP_1[4] = 40;
   if(TP_1.empty()) {
      cout<<"Map is NULL";
   } else {
      cout<<"Map isn't NULL";
   }
   return 0;
}

Output

Map isn't NULL

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main() {
   map<int, int> TP_1;
   map<int, int> TP_2;
   TP_1[1] = 10;
   TP_1[2] = 20;
   TP_1[3] = 30;
   TP_1[4] = 40;
   if(TP_1.empty()) {
      cout<<"Map_1 is NULL";
   } else {
      cout<<"Map_1 isn't NULL";
   }
   if(TP_2.empty()) {
      cout<<"\nMap_2 is NULL";
   } else {
      cout<<"Map_2 isn't NULL";
   }
   return 0;
}

Output

Map_1 isn't NULL
Map_2 is NULL

Updated on: 15-Apr-2020

378 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements