Queue are a type of container adaptors which 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.
Syntax :
queuename.empty() Parameters : No parameters are passed Returns : True, if list is empty False, Otherwise
Examples:
Input : myqueue = 1, 2, 3
myqueue.empty();
Output : False
Input : myqueue
myqueue.empty();
Output : True
Errors and Exceptions
- Shows error if a parameter is passed
- Shows no exception throw guarantee.
// 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; } |
Output:
False
Application : Given a queue of integers, find the sum of the all the integers.
Input : 1, 8, 3, 6, 2 Output : 20
Algorithm
1. Check if the queue is empty, if not add the front element to a variable initialised as 0, and pop the front element.
2. Repeat this step until the queue is empty.
3. Print the final value of the variable.
// 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; } |
Output:
20
size() function is used to return the size of the list container or the number of elements in the list container.
Syntax :
queuename.size() Parameters : No parameters are passed Returns : Number of elements in the container
Examples:
Input : myqueue = 1, 2, 3
myqueue.size();
Output : 3
Input : myqueue
myqueue.size();
Output : 0
Errors and Exceptions
- Shows error if a parameter is passed.
- Shows no exception throw guarantee
// 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; } |
Output:
5
Application : Given a queue of integers, find the sum of the all the integers.
Input : 1, 8, 3, 6, 2 Output : 20
Algorithm
1. Check if the size of the queue is zero, if not add the front element to a variable initialised as 0, and pop the front element.
2. Repeat this step until the queue size becomes 0.
3. Print the final value of the variable.
// 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; } |
Output:
20
Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.
Recommended Posts:
- fill() and fill_n() functions in C++ STL
- Array algorithms in C++ STL (all_of, any_of, none_of, copy_n and iota)
- accumulate() and partial_sum() in C++ STL : numeric header
- numeric header in C++ STL | Set 2 (adjacent_difference(), inner_product() and iota())
- Graph implementation using STL for competitive programming | Set 1 (DFS of Unweighted and Undirected)
- Binary Search functions in C++ STL (binary_search, lower_bound and upper_bound)
- Descending order in Map and Multimap of C++ STL
- Insertion and Deletion in STL Set C++
- std::minmax() and std::minmax_element() in C++ STL
- deque::at() and deque::swap() in C++ STL
- list::push_front() and list::push_back() in C++ STL
- list::pop_front() and list::pop_back() in C++ STL
- list::front() and list::back() in C++ STL
- list::empty() and list::size() in C++ STL
- list::remove() and list::remove_if() in C++ STL
- queue::push() and queue::pop() in C++ STL
- deque::pop_front() and deque::pop_back() in C++ STL
- stack empty() and stack size() in C++ STL
- deque::front() and deque::back() in C++ STL
- deque::empty() and deque::size() in C++ STL
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

