C++ Standard Library C++ STL Library

C++ <list> - empty() Function



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

Time Complexity

Constant i.e, Θ(1)

Example:

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

#include <iostream>
#include <list>
using namespace std;
 
int main (){
  list<int> MyList;

  cout<<boolalpha;

  cout<<"Is the List empty?: "<<MyList.empty()<<"\n";

  cout<<"Add elements in the List.\n";
  MyList.push_back(10);
  MyList.push_back(20);
  MyList.push_back(30);

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

The output of the above code will be:

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

❮ C++ <list> Library

5