queue::size() method

Queues are an abstract data type, specifically designed to operate in a FIFO context (first-in-first-out). The elements are inserted into one end of the container and extracted from the other. They are a type of container adaptor in the C++ Standard Library. The queue::size() is a method available in the STL which returns the number of elements in the queue

Syntax:

queue_name.size();

Parameters: The queue::size() method does not accept any parameter
Return value: Number of elements in the queue

Example of queue::size() method

#include <iostream>
#include <queue>

using namespace std;

int main ()
{
  queue<int> q1;

  for(int i = 1; i <= 5; i++)
    q1.push(i);

  cout<<"Size of queue: "<<q1.size();
  return 0;
}

Output:

Size of queue: 5

This article is written by

Please comment below, if you have any doubts or find any error in the above article.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top