Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c++
communitycreator

What is queue.empty() in C++?

Harris Amjad

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The queue.empty() function in C++ returns a true (1) value if the queue is empty. Otherwise, it returns false (0). In short, this function is used to check if the queue is empty or not.

Figure 1 shows the visual representation of the queue.empty() function.

Figure 1: Visual representation of queue.empty() function

In order to use the queue.empty() function, we must include queue in the program, as shown below:

#include <queue>

Syntax

queue_name.empty()
// where the queue_name is the name of the queue

Parameter

This function does not require a parameter.

Return value

If the queue is empty, the queue.empty() function returns a true (1) value. Otherwise, it returns false (0).

Example

#include <iostream>
//header file
#include <queue>
using namespace std;
int main() {
//filled queue
queue<int> queue1;
queue1.push(1);
queue1.push(3);
queue1.push(5);
queue1.push(2);
queue1.push(0);
//queue1 = 1->3->5->2->0
cout<<"The queue1 is empty: \n"<<queue1.empty()<<"\n";
//empty queue
queue<int> queue2;
cout<<"The queue2 is empty: \n"<<queue2.empty();
}

Explanation

In the code above, we have two queues, queue1 and queue2. In queue1, we push five items so that it is not empty anymore. Therefore, when we call the empty() function on queue1, the function returns 0. However, when we call the empty function on queue2, which is empty, the function returns 1.

RELATED TAGS

c++
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring