C++ Standard Library C++ STL Library

C++ priority_queue - empty() Function



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

Syntax

bool empty() const;
bool empty() const;

Parameters

No parameter is required.

Return Value

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

Time Complexity

Constant i.e, Θ(1).

Example:

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

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

  cout<<boolalpha;

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

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

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

The output of the above code will be:

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

❮ C++ <queue> Library