queue::empty() 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::empty() is a method available in the STL which returns whether the queue is empty.

Syntax:

queue_name.empty();

Parameters: The queue::empty() method does not accept any parameter
Return value: true if size is zero, else false

Example of queue::empty() method

#include <iostream>
#include <queue>

using namespace std;

void check_empty(queue<int> q)
{
  if(q.empty())
  {
      cout<<"True";
  }

  else
  {
      cout<<"False";
  }
}

int main ()
{
  queue<int> q1;

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

  cout<<"Q1 empty?  ";
  check_empty(q1);

  cout<<endl;

  queue<int> q2;
  cout<<"Q2 empty?  ";
  check_empty(q2);

  return 0;
}

Output:

Q1 empty?  False
Q2 empty?  True

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