C++ Standard Library C++ STL Library

C++ multimap - empty() Function



The C++ multimap::empty function is used to check whether the multimap is empty or not. It returns true if the size of the multimap is zero, else returns false.

Syntax

bool empty() const;
bool empty() const noexcept;

Parameters

No parameter is required.

Return Value

true if the size of the multimap is zero, else returns false.

Time Complexity

Constant i.e, Θ(1).

Example:

In the example below, the multimap::empty function is used to check whether the multimap is empty or not.

#include <iostream>
#include <map>
using namespace std;
 
int main (){
  multimap<string, string> MyMMap;

  cout<<boolalpha;

  cout<<"Is the Multimap empty?: "<<MyMMap.empty()<<"\n";

  cout<<"Add key/element pairs in the Multimap.\n";

  MyMMap.insert(pair<string, string>("USA", "New York"));
  MyMMap.insert(pair<string, string>("USA", "Washington"));  
  MyMMap.insert(pair<string, string>("CAN", "Toronto"));

  cout<<"Now, Is the Multimap empty?: "<<MyMMap.empty()<<"\n";
  return 0;
}

The output of the above code will be:

Is the Multimap empty?: true
Add key/element pairs in the Multimap.
Now, Is the Multimap empty?: false

❮ C++ <map> Library