Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Deque::empty() and deque::size() in C++ STL

  • Difficulty Level : Basic
  • Last Updated : 25 Nov, 2021

Deque or Double Ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also at the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed in the deque.

deque::empty()

empty() function is used to check if the deque container is empty or not. This is an inbuilt function from C++ Standard Template Library(STL). This function belongs to the <deque> header file. The function either returns true or false, depending on whether the deque is empty or not.
Syntax : 

dequename.empty()

Returns: True, if deque is empty. False, Otherwise

Examples: 

Input :   mydeque
          mydeque.empty();
Output :  True
 
Input :   mydeque = 1, 2, 3
          mydeque.empty();
Output :  False

Errors and Exceptions:

  • Shows error if the parameter is passed. 
  • Shows no exception throw guarantee.

CPP




// CPP program to demonstrate
// Implementation of empty() function
#include <deque>
#include <iostream>
using namespace std;
  
// Driver Code
int main()
{
    deque<int> mydeque;
    mydeque.push_front(1);
  
    // deque becomes 1
    if (mydeque.empty()) {
        cout << "True";
    }
    else {
        cout << "False";
    }
    return 0;
}
Output
False

Application: deque::empty()

Given a deque of integers, find the sum of the all the integers. 

Input : 1, 8, 3, 6, 2
Output: 20

Algorithm: 

1. Check if the deque 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 deque is empty. 
3. Print the final value of the variable.

CPP




// CPP program to demonstrate
// Application of empty() function
#include <deque>
#include <iostream>
using namespace std;
  
// Driver Code
int main()
{
    int sum = 0;
    deque<int> mydeque;
    mydeque.push_back(1);
    mydeque.push_back(8);
    mydeque.push_back(3);
    mydeque.push_back(6);
    mydeque.push_back(2);
    // deque becomes 1, 8, 3, 6, 2
  
    while (!mydeque.empty()) {
        sum = sum + mydeque.front();
        mydeque.pop_front();
    }
    cout << sum;
    return 0;
}
Output
20

deque::size()

size() function is used to return the size of the deque container or the number of elements in the deque container. This is an inbuilt function from C++ Standard Template Library(STL). This function belongs to the <deque> header file. The function either returns a number demonstrating the total elements the deque holds at that instance.
Syntax : 

dequename.size()

Returns: g>Number of elements in the container.

Examples: 

Input :   mydeque = 0, 1, 2
          mydeque.size();
Output :  3
 
Input :   mydeque = 0, 1, 2, 3, 4, 5
          mydeque.size();
Output :  6

Errors and Exceptions:

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

CPP




// CPP program to demonstrate
// Implementation of size() function
#include <deque>
#include <iostream>
using namespace std;
  
// Driver Code
int main()
{
    int sum = 0;
    deque<int> mydeque;
    mydeque.push_back(1);
    mydeque.push_back(8);
    mydeque.push_back(3);
    mydeque.push_back(6);
    mydeque.push_back(2);
    // deque becomes 1, 8, 3, 6, 2
  
    cout << mydeque.size();
  
    return 0;
}
Output
5

Application: deque::size()

Given a deque of integers, find the sum of all the integers. 

Input : 1, 8, 3, 6, 2
Output: 20

Algorithm: 

1. Check if the size of the deque 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 deque size becomes 0. 
3. Print the final value of the variable.

CPP




// CPP program to demonstrate
// Application of size() function
#include <deque>
#include <iostream>
using namespace std;
  
// Driver Code
int main()
{
    int sum = 0;
    deque<int> mydeque;
    mydeque.push_back(1);
    mydeque.push_back(8);
    mydeque.push_back(3);
    mydeque.push_back(6);
    mydeque.push_back(2);
    // Deque becomes 1, 8, 3, 6, 2
  
    while (mydeque.size() > 0) {
        sum = sum + mydeque.front();
        mydeque.pop_front();
    }
    cout << sum;
    return 0;
}
Output
20

Note: Basically, empty() function checks if the size() of the deque is zero or not. 

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!