Queue Empty Function - C++ Programming Tutorial
C++ Course / Data Structures / Queue Empty Function

Queue Empty Function

BLUF: Mastering Queue Empty Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Queue Empty Function

C++ is renowned for its efficiency. Learn how Queue Empty Function enables low-level control and high-performance computing in the tutorial below.

The C++ Queue empty method is employed to check if the container is devoid of any elements. It can be practical to verify if the container is empty before manipulating its elements, making this function valuable in such scenarios.

Syntax

Example

bool empty() const;

Parameters

There are no arguments provided for this function. Its sole purpose is to check if the container is empty, therefore no parameters are required for its execution.

Return value

If the specified container is devoid of content, the function will return 'true'; otherwise, it will return 'false'.

Example 1

Example

#include <iostream>
#include <queue>
int main()
{
	std::queue<int> newqueue;
	int result=0;
	for (int j=1; j<=10; j++)
	newqueue.push(j);
	while (!newqueue.empty () )
	{
		result += newqueue.front ();
		newqueue.pop();
	}
	std::cout << "result is: " << result;
	return 0;
}

Output:

Output

result is: 55

Example 2

Example

#include <iostream>
#include <queue>
using namespace std;
int main()
{
	queue<int> newqueue;
	newqueue.push(55);
	if(newqueue.empty())
	{
		cout<<"The queue is empty";
	}
	else
	{
		cout<<"The queue is not empty";
	}
	return 0;
}

Output:

Output

The queue is  not empty

Complexity

The complexity of the function is constant.

Data races

Only the collection is interacted with. By interacting with the collection, we can determine its emptiness and use this information to decide the return value.

Exception Safety

A guarantee is given that matches the operations carried out on the container object.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience