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


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

Syntax

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

Parameters

No parameter is required.

Return Value

Number of elements present in the queue.

Time Complexity

Constant i.e, Θ(1).

Example:

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

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

  //add elements in the queue
  MyQueue.push(10);
  MyQueue.push(20);
  MyQueue.push(30);
  MyQueue.push(40);
  MyQueue.push(50);

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

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

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

The output of the above code will be:

Queue size is: 5

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

❮ C++ - Queue