C++ Standard Library C++ STL Library

C++ unordered_set - empty() Function



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

Syntax

bool empty() const noexcept;

Parameters

No parameter is required.

Return Value

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

Time Complexity

Constant i.e, Θ(1).

Example:

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

#include <iostream>
#include <unordered_set>
using namespace std;
 
int main (){
  unordered_set<int> uSet;

  cout<<boolalpha;

  cout<<"Is the Unordered Set empty?: "<<uSet.empty()<<"\n";

  cout<<"Add elements in the Unordered Set:\n";
  uSet.insert(10);
  uSet.insert(20);
  uSet.insert(30);

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

The output of the above code will be:

Is the Unordered Set empty?: true
Add elements in the Unordered Set:
Now, Is the Unordered Set empty?: false

❮ C++ <unordered_set> Library

5