Last Updated : 04 Jun, 2023
Queue is a type of container adaptor that operate in a first in first out (FIFO) type of arrangement. Elements are inserted at the back (end) and are deleted from the front.
queue::empty()empty() function is used to check if the queue container is empty or not.
SyntaxqueueName.empty()Parameters
Input: myqueue = 1, 2, 3 myqueue.empty(); Output: False Input: myqueue myqueue.empty(); Output: TrueErrors and Exceptions
The below C++ demonstrates the use of the empty() function in C++ STL's queue class.
C++
// CPP program to illustrate
// Implementation of empty() function
#include <iostream>
#include <queue>
using namespace std;
int main()
{
queue<int> myqueue;
myqueue.push(1);
// Queue becomes 1
if (myqueue.empty()) {
cout << "True";
}
else {
cout << "False";
}
return 0;
}
Example 2
The below problem statement demonstrates one of the applications of the empty() function in C++ STL's queue class.
Given a queue of integers, the task is to find the sum of all the integers.
Sample Input: 1, 8, 3, 6, 2
Sample Output: 20
Algorithm
Below is the implementation:
C++
// CPP program to illustrate
// Application of empty() function
#include <iostream>
#include <queue>
using namespace std;
int main()
{
int sum = 0;
queue<int> myqueue;
myqueue.push(1);
myqueue.push(8);
myqueue.push(3);
myqueue.push(6);
myqueue.push(2);
// Queue becomes 1, 8, 3, 6, 2
while (!myqueue.empty()) {
sum = sum + myqueue.front();
myqueue.pop();
}
cout << sum;
return 0;
}
queue::size()
size() function is used to return the size of the list container which is the number of elements currently stored in the list container.
Syntaxqueuename.size()Parameters
Input : myqueue = 1, 2, 3 myqueue.size(); Output : 3 Input : myqueue myqueue.size(); Output : 0Errors and Exceptions
The below C++ demonstrates the use of the size() function in C++ STL's queue class.
C++
// CPP program to illustrate
// Implementation of size() function
#include <iostream>
#include <queue>
using namespace std;
int main()
{
int sum = 0;
queue<int> myqueue;
myqueue.push(1);
myqueue.push(8);
myqueue.push(3);
myqueue.push(6);
myqueue.push(2);
// Queue becomes 1, 8, 3, 6, 2
cout << myqueue.size();
return 0;
}
Example 2
The below problem statement demonstrates one of the applications of the size() function in C++ STL's queue class.
Given a queue of integers, find the sum of all the integers.
Sample Input : 1, 8, 3, 6, 2
Sample Output: 20
Algorithm
Below is the implementation:
C++
// CPP program to illustrate
// Application of empty() function
#include <iostream>
#include <queue>
using namespace std;
int main()
{
int sum = 0;
queue<int> myqueue;
myqueue.push(1);
myqueue.push(8);
myqueue.push(3);
myqueue.push(6);
myqueue.push(2);
// Queue becomes 1, 8, 3, 6, 2
while (myqueue.size() > 0) {
sum = sum + myqueue.front();
myqueue.pop();
}
cout << sum;
return 0;
}
Let us see the differences in a tabular form.
S.No. queue::empty() queue::size() 1. It is used to return whether the queue is empty. It is used to return the number of elements in the queue. 2. Its syntax is -:RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4