Tutorialspoint.dev

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

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 1, 2, 3, 4, 5, 6, 7, 8
  
    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


This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter