C++ Standard Library C++ STL Library

C++ set - empty() Function



The C++ set::empty function is used to check whether the set is empty or not. It returns true if the size of the set 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 set is zero, else returns false.

Time Complexity

Constant i.e, Θ(1).

Example:

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

#include <iostream>
#include <set>
using namespace std;
 
int main (){
  set<int> MySet;

  cout<<boolalpha;

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

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

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

The output of the above code will be:

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

❮ C++ <set> Library