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


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 below example, the priority_queue::size function is used find out the total number of elements in a priority_queue called p_queue.

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

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

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

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

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

The output of the above code will be:

Priority Queue size is: 5

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

❮ C++ - Priority Queue