multimap empty() function in C++ STL


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

What is Multimap in C++ STL?

Multimaps are the associative containers, which are similar to map containers. It also facilitates to store the elements formed by a combination of key-value and mapped value in a specific order. In a multimap container there can be multiple elements associated with the same key. The data is internally always sorted with the help of its associated keys.

What is multimap::empty()?

multimap::empty() function is an inbuilt function in C++ STL, which is defined in <map>header file. empty() is used to check whether the associated multimap 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.

Input 

multimap<char, int > newmap;
newmap.insert(make_pair(‘A’, 10));
newmap.insert(make_pair(‘B’, 20));
newmap.insert(make_pair(‘C’, 30));
mymap.empty();

Output 

false

Input 

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

Output 

true

Example

 Live Demo

#include<iostream>
#include<map>
using namespace std;
int main(){
   multimap<int,int > mul_1;
   //inserting elements to multimap1
   mul_1.insert({1,10});
   mul_1.insert({2,20});
   mul_1.insert({3,30});
   mul_1.insert({4,40});
   mul_1.insert({5,50});
   if(mul_1.empty()){
      cout<<"\nMultimap is empty";
   }
   else{
      cout<<"\nMultimap is not empty";
      cout<<"\nElements in multimap1 are: "<<"\n";
      for(auto i= mul_1.begin(); i!= mul_1.end(); i++){
         cout<<(*i).first<<" "<< (*i).second << "\n";
      }
   }
}

Output

If we run the above code it will generate the following output −

Multimap is not empty
Elements in multimap1 are:
1 10
2 20
3 30
4 40
5 50

Updated on: 22-Apr-2020

151 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements