 GeeksforGeeks App
Open App Browser
Continue

# 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 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::front()

front() is used to reference the first element of the deque container. This function can be used to fetch the first element of a deque. This is an inbuilt function from C++ Standard Template Library(STL). This function belongs to the <deque> header file.

Syntax :

`dequename.front()`

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 behavior.
2. It has a no exception throw guarantee if the deque is not empty.

## C++

 `// CPP program to demonstrate``// Implementation of front() function``#include ``#include ``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`

Time complexity: O(1)

Auxiliary Space: O(1)

### deque::back()

back() 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. This function can be used to fetch the first element of a deque. This is an inbuilt function from C++ Standard Template Library(STL). This function belongs to the <deque> header file.

Syntax :

`dequename.back()`

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 behavior.
2. It has a no exception throw guarantee if the deque is not empty.

## C++

 `// CPP program to demonstrate``// Implementation of back() function``#include ``#include ``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`

Time complexity: O(1)

Auxiliary Space: O(1)

### Application: deque::front() and deque::back()

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: The last element is 8, the first element is 1, Difference will be 7)

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

## C++

 `// CPP program to demonstrate``// application Of front() and back() function``#include ``#include ``using` `namespace` `std;` `// Driver Code``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"``;` `    ``return` `0;``}`

Output

`7`

Time complexity: O(1)

Auxiliary Space: O(1)

Let us see the differences in a tabular form -: