Facebook Page Twitter Page LinkedIn Page
× C++ Standard Library C++ STL Library


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 below example, 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> p_queue;

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

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

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

The output of the above code will be:

Is the Priority Queue empty?: 1

Add three elements in the Priority Queue.
Now, Is the Priority Queue empty?: 0

❮ C++ - Priority Queue