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
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
#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:
result is: 55
Example 2
#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:
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.