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>

usingnamespacestd;

intmain()

{

intsum = 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;

return0;

}

Output:

20

queue::size()

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

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.

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.