Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

queue::empty() and queue::size() in C++ STL

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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.

queue::empty()

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

  1. Shows error if a parameter is passed
  2. Shows no exception throw guarantee.

CPP




// 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 initialized 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




// 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
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

Examples:

Input :  myqueue = 1, 2, 3
         myqueue.size();
Output : 3

Input :  myqueue
         myqueue.size();
Output : 0

Errors and Exceptions

  1. Shows error if a parameter is passed.
  2. Shows no exception throw guarantee

CPP




// 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 initialized 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




// 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

Let us see the differences in a tabular form -:

 queue::empty() queue::size()
1.It is used to return whether the queue is emptyIt 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.

My Personal Notes arrow_drop_up
Last Updated : 24 May, 2023
Like Article
Save Article
Similar Reads