Facebook Page Twitter Page LinkedIn Page
× C++ Standard Library C++ STL Library


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

Syntax

bool empty() const noexcept;

Parameters

No parameter is required.

Return Value

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

Time Complexity

Constant i.e, Θ(1).

Example:

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

#include <iostream>
#include <unordered_map>
using namespace std;
 
int main (){
  unordered_map<int, string> uMap;

  cout<<"Is the Unordered Map empty?: "<<uMap.empty()<<"\n\n";

  cout<<"Add three key/element pairs in the Unordered Map:\n";

  uMap[101] = "John";
  uMap[102] = "Marry";
  uMap[103] = "Kim";

  cout<<"Now, Is the Unordered Map empty?: "<<uMap.empty()<<"\n";
  return 0;
}

The output of the above code will be:

Is the Unordered Map empty?: 1

Add three key/element pairs in the Unordered Map:
Now, Is the Unordered Map empty?: 0

❮ C++ - Unordered Map