Skip to content
Related Articles

Related Articles

Improve Article

deque::front() and deque::back() in C++ STL

  • Difficulty Level : Basic
  • Last Updated : 09 Aug, 2019

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

deque::front()

This function is used to reference the first element of the deque container. This function can be used to fetch the first element of a deque.

Syntax :

dequename.front()
Parameters :
No value is needed to pass as the parameter.
Returns :
Direct reference to the first element of the deque container.

Examples:

Input  :  mydeque = 1, 2, 3
          mydeque.front();
Output :  1

Input  :  mydeque = 3, 4, 1, 7, 3
          mydeque.front();
Output :  3

Errors and Exceptions



  1. If the deque container is empty, it causes undefined behaviour
  2. It has a no exception throw guarantee if the deque is not empty




// CPP program to illustrate
// Implementation of front() function
#include <deque>
#include <iostream>
using namespace std;
  
int main()
{
    deque<int> mydeque;
    mydeque.push_back(3);
    mydeque.push_back(4);
    mydeque.push_back(1);
    mydeque.push_back(7);
    mydeque.push_back(3);
    // Queue becomes 3, 4, 1, 7, 3
  
    cout << mydeque.front();
    return 0;
}

Output:

3
deque::back()

This function is used to reference the last element of the deque container. This function can be used to fetch the first element from the back of a deque.

Syntax :

dequename.back()
Parameters :
No value is needed to pass as the parameter.
Returns :
Direct reference to the last element of the deque container.

Examples:

Input  :  mydeque = 1, 2, 3
          mydeque.back();
Output :  3

Input  :  mydeque = 3, 4, 1, 7, 3
          mydeque.back();
Output :  3

Errors and Exceptions

  1. If the deque container is empty, it causes undefined behaviour
  2. It has a no exception throw guarantee if the deque is not empty




// CPP program to illustrate
// Implementation of back() function
#include <deque>
#include <iostream>
using namespace std;
  
int main()
{
    deque<int> mydeque;
    mydeque.push_back(3);
    mydeque.push_back(4);
    mydeque.push_back(1);
    mydeque.push_back(7);
    mydeque.push_back(3);
    // Queue becomes 3, 4, 1, 7, 3
  
    cout << mydeque.back();
    return 0;
}

Output:

3

Application :
Given an empty deque of integers, add numbers to the deque, then print the difference between the first and the last element.

Input  : 1, 2, 3, 4, 5, 6, 7, 8
Output : 7
Explanation - Last element = 8, First element = 1, Difference = 7

Algorithm
1. Add numbers to the deque using push_back() function
2. Compare the first and the last element.
3. If first element is larger, subtract last element from it and print it.
4. Else subtract first element from the last element and print it.




// CPP program to illustrate
// application Of front() and back() function
#include <deque>
#include <iostream>
using namespace std;
  
int main()
{
    deque<int> mydeque;
    mydeque.push_back(8);
    mydeque.push_back(7);
    mydeque.push_back(6);
    mydeque.push_back(5);
    mydeque.push_back(4);
    mydeque.push_back(3);
    mydeque.push_back(2);
    mydeque.push_back(1);
  
    // deque becomes 8, 7, 6, 5, 4, 3, 2, 1
  
    if (mydeque.front() > mydeque.back()) {
        cout << mydeque.front() - mydeque.back();
    }
    else if (mydeque.front() < mydeque.back()) {
        cout << mydeque.back() - mydeque.front();
    }
    else
        cout << "0";
}

Output:

7
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :