C++ Standard Library C++ STL Library

C++ queue - empty() Function



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

Time Complexity

Constant i.e, Θ(1).

Example:

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

#include <iostream>
#include <queue>
using namespace std;
 
int main (){
  queue<int> MyQueue;

  cout<<boolalpha;

  cout<<"Is the Queue empty?: "<<MyQueue.empty()<<"\n";

  cout<<"Add elements in the Queue.\n";
  MyQueue.push(10);
  MyQueue.push(20);
  MyQueue.push(30);

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

The output of the above code will be:

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

❮ C++ <queue> Library