C++ Standard Library C++ STL Library

C++ priority_queue - size() Function



The C++ priority_queue::size function is used to find out the total number of elements in the priority_queue.

Syntax

size_type size() const;
size_type size() const;

Parameters

No parameter is required.

Return Value

Number of elements present in the priority_queue.

Time Complexity

Constant i.e, Θ(1).

Example:

In the example below, the priority_queue::size function is used find out the total number of elements in a priority_queue called pqueue.

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

  //add elements in the priority_queue
  pqueue.push(10);
  pqueue.push(20);
  pqueue.push(30);
  pqueue.push(40);
  pqueue.push(50);

  cout<<"Priority Queue size is: "<<pqueue.size()<<"\n";

  cout<<"Three elements are added in the Priority Queue.\n";
  pqueue.push(60);
  pqueue.push(70);
  pqueue.push(80);

  cout<<"Now, Priority Queue size is: "<<pqueue.size()<<"\n";
  return 0;
}

The output of the above code will be:

Priority Queue size is: 5
Three elements are added in the Priority Queue.
Now, Priority Queue size is: 8

❮ C++ <queue> Library