Home
Aptitude
Categories


Home » C++ STL

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

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.

Program:

#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

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.