Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

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

  • Difficulty Level : Basic
  • Last Updated : 13 Jan, 2018


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.

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

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.




// 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;
}

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.




// 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;
}

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




// 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;
}

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.




// 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;
}

Output:

20



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!