×

C++ STL Tutorial

C++ STL Algorithm

C++ STL Arrays

C++ STL String

C++ STL List

C++ STL Stack

C++ STL Set

C++ STL Queue

C++ STL Vector

C++ STL Map

C++ STL Multimap

C++ STL MISC.

Check if a queue is empty or not using queue::size() function

Check queue is empty or not in C++ STL: Here, we are going to learn check whether a given queue is an empty queue or not using C++ STL?
Submitted by IncludeHelp, on November 27, 2018

Check if a queue is empty or not

There is a function queue::empty() that can be used to check whether queue is empty or not – it returns 1 (true) if queue is empty else returns 0 (false).

But, in this example – we are checking it by using queue::size() function.

If we don't want to use queue::empty() function, we can check the size of the queue, if it is 0 – queue is an empty queue, if it is not 0 (greatest than zero), queue is empty.

C++ example to check if a queue is empty or not using

#include <iostream>
#include <queue>
using namespace std;

// Main fubction
int main() {
  // declaring two queues
  queue<int> Q1;
  queue<int> Q2;

  // inserting elements to Q1
  Q1.push(10);
  Q1.push(20);
  Q1.push(30);

  if (Q1.size() == 0)
    cout << "Q1 is an empty queue" << endl;
  else
    cout << "Q1 is not an empty queue" << endl;

  if (Q2.size() == 0)
    cout << "Q2 is an empty queue" << endl;
  else
    cout << "Q2 is not an empty queue" << endl;

  return 0;
}

Output

Q1 is not an empty queue 
Q2 is an empty queue 

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.