priority_queue::empty() and priority_queue::size() in C++ STL


Priority queues are a type of container adaptors, specifically designed such that the first element of the queue is the greatest of all elements in the queue.

priority_queue::empty()

empty() function is used to check if the priority queue container is empty or not.

Syntax :

pqueuename.empty()
Parameters :
No parameters are passed
Returns :
True, if priority queue is empty, 
False, Otherwise

Examples:

Input :  pqueue = 3, 2, 1
         pqueue.empty();
Output : False

Input :  pqueue
         pqueue.empty();
Output : True

Errors and Exceptions

1. Shows error if a parameter is passed
2. Shows no exception throw guarantee.

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to illustrate
// Implementation of empty() function
#include <iostream>
#include <queue>
using namespace std;
  
int main()
{
    priority_queue<int> pqueue;
    pqueue.push(1);
  
    // Priority Queue becomes 1
  
    if (pqueue.empty()) {
        cout << "True";
    }
    else {
        cout << "False";
    }
    return 0;
}

chevron_right


Output:

False

Application : Given a priority queue of integers, find the sum of the all the integers.

Input  : 8, 6, 3, 2, 1 
Output : 20

Algorithm
1. Check if the priority queue is empty, if not add the top element to a variable initialised as 0, and pop the top element.
2. Repeat this step until the priority queue is empty.
3. Print the final value of the variable.

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to illustrate
// Application of empty() function
#include <iostream>
#include <queue>
using namespace std;
  
int main()
{
    int sum = 0;
    priority_queue<int> pqueue;
    pqueue.push(8);
    pqueue.push(6);
    pqueue.push(3);
    pqueue.push(2);
    pqueue.push(1);
  
    // Queue becomes 8, 6, 3, 2, 1
  
    while (!pqueue.empty()) {
        sum = sum + pqueue.top();
        pqueue.pop();
    }
    cout << sum;
    return 0;
}

chevron_right


Output:

20
priority_queue::size()

size() function is used to return the size of the priority queue container or the number of elements in the container.

Syntax :

pqueuename.size()
Parameters :
No parameters are passed
Returns :
Number of elements in the container

Examples:

Input :  pqueue = 3, 2, 1
         pqueue.size();
Output : 3

Input :  pqueue
         pqueue.size();
Output : 0

Errors and Exceptions

1. Shows error if a parameter is passed.
2. Shows no exception throw guarantee

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to illustrate
// Implementation of size() function
#include <iostream>
#include <queue>
using namespace std;
  
int main()
{
    int sum = 0;
    priority_queue<int> pqueue;
    pqueue.push(8);
    pqueue.push(6);
    pqueue.push(3);
    pqueue.push(3);
    pqueue.push(1);
  
    // Priority Queue becomes 8, 6, 3, 2, 1
  
    cout << pqueue.size();
  
    return 0;
}

chevron_right


Output:

5

Application : Given a priority queue of integers, find the sum of the all the integers.

Input  : 8, 6, 3, 2, 1 
Output : 20

Algorithm
1. Check if the size of the priority queue is 0, if not add the top element to a variable initialised as 0, and pop the top element.
2. Repeat this step until the sizeof the priority queue becomes 0.
3. Print the final value of the variable.

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to illustrate
// Application of size() function
#include <iostream>
#include <queue>
using namespace std;
  
int main()
{
    int sum = 0;
    priority_queue<int> pqueue;
    pqueue.push(8);
    pqueue.push(6);
    pqueue.push(3);
    pqueue.push(2);
    pqueue.push(1);
  
    // Queue becomes 8, 6, 3, 2, 1
  
    while (pqueue.size() > 0) {
        sum = sum + pqueue.top();
        pqueue.pop();
    }
    cout << sum;
    return 0;
}

chevron_right


Output:

20


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.