C++ Standard Library C++ STL Library

C++ <forward_list> - empty() Function



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

Syntax

bool empty() const noexcept;

Parameters

No parameter is required.

Return Value

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

Time Complexity

Constant i.e, Θ(1).

Example:

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

#include <iostream>
#include <forward_list>
using namespace std;
 
int main (){
  forward_list<int> flist;

  cout<<boolalpha;

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

  cout<<"Add elements in the Forward List.\n";
  flist.push_front(10);
  flist.push_front(20);
  flist.push_front(30);

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

The output of the above code will be:

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

❮ C++ <forward_list> Library