![]() |
VOOZH | about |
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.
empty() function is used to check if the queue container is empty or not.
queueName.empty()
Input: myqueue = 1, 2, 3 myqueue.empty(); Output: False Input: myqueue myqueue.empty(); Output: True
The below C++ demonstrates the use of the empty() function in C++ STL's queue class.
False
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:
20
size() function is used to return the size of the list container which is the number of elements currently stored in the list container.
queuename.size()
Input : myqueue = 1, 2, 3 myqueue.size(); Output : 3 Input : myqueue myqueue.size(); Output : 0
The below C++ demonstrates the use of the size() function in C++ STL's queue class.
5
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:
20
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 -: empty(); | Its syntax is -: size(); |
| 3. | It does not take any parameters. | It does not take any parameters. |
| 4. | Its return type is of boolean. | Its return type is an integer. |
| 5. | Its complexity is constant. | Its complexity is constant. |