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:
- If the deque container is empty, it causes undefined behavior.
- It has a no exception throw guarantee if the deque is not empty.
C++
#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);
cout << mydeque.front();
return 0;
}
|
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:
- If the deque container is empty, it causes undefined behavior.
- It has a no exception throw guarantee if the deque is not empty.
C++
#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);
cout << mydeque.back();
return 0;
}
|
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++
#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);
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;
}
|
Time complexity: O(1)
Auxiliary Space: O(1)
Let us see the differences in a tabular form -:
| Deque::front() | deque::back() |
1. | It is used to return a reference to the first element in the deque container. | It is used to return a reference to the last element in the container. |
2. | Its syntax is -: reference front(); | Its syntax is -: reference back(); |
3. | It does not take any parameters. | It does not take any parameters. |
4. | Its complexity is constant. | Its complexity is constant. |
5. | Its iterator validity does not change. | Its iterator validity does not change. |
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.