priority_queue::empty() method

A priority queue is a dynamically resizing container adopter in the C++ Standard Library. Elements in a priority queue are arranged in non-decreasing order such that the first element is always the greatest element in the queue. The priority_queue::empty() is a method available in the STL which returns whether the priority queue is empty i.e. whether its size is zero.

Syntax:

priorityq_name.empty();

Parameter: The priority_queue::empty() does not accept any parameter
Return value: true if the size is 0, false otherwise.

Example of priority_queue::empty() method

#include <iostream>>
#include <queue>

using namespace std;

void check(priority_queue <int> q) //helper function to print whether empty queue or not
{

    if(q.empty())
    {
        cout<<"true";
    }

    else
    {
        cout<<"false";
    }

}


int main ()
{
    priority_queue<int> q1;

    q1.push(10);
    q1.push(20);
    q1.push(40);
    q1.push(30);
    q1.push(50);

    cout<<"Q1 is empty? ";
    check(q1);

    cout<<endl;
    priority_queue<int> q2;

    cout<<"Q2 is empty? ";
    check(q2);
    return 0;
}

Output:

Q1 is empty? false
Q2 is empty? true

This article is written by

Please comment below, if you have any doubts or find any error in the above article.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top