Deque::front() and deque::back() in C++ STL
Last Updated :
11 Sep, 2023
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++
// CPP program to demonstrate
// 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;
}
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++
// CPP program to demonstrate
// 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;
}
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 <deque>
#include <iostream>
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;
}
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. |
Similar Reads
Deque in C++ STL In C++, deque container provides fast insertion and deletion at both ends. Stands for Double Ended QUEue, it is a special type of queue where insertion and deletion operations are possible at both the ends in constant time complexity.Example:C++#include <iostream> #include <deque> using
6 min read
Commonly Used Methods
deque::push_front() in C++ STLDeque 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
2 min read
deque::push_back() in C++ STLDeque 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
2 min read
deque::pop_front() and deque::pop_back() in C++ STLDeque 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
4 min read
Deque::front() and deque::back() in C++ STLDeque 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
4 min read
deque insert() function in C++ STLThe deque::insert() function is a built-in function in C++ which is used to insert elements in the deque. The insert() function can be used in three ways: Extends deque by inserting a new element val at a position.Extends deque by inserting n new element of value val in the deque.Extends deque by in
3 min read
deque::begin() and deque::end in C++ STLDeque 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
3 min read
Deque::empty() and deque::size() in C++ STLDeque 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
4 min read
deque::clear() and deque::erase() in C++ STLDeque 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 the case of insertion and deletion of elements at the end, and also at the beginning. Unlike vectors, contiguous storage allocation
5 min read
Other Member Methods
deque max_size() function in C++ STLThe deque::max_size() is a built-in function in C++ STL which returns the maximum number of elements that a deque container can hold. Syntax: deque_name.max_size()Parameters: The function does not accept any parameters. Return Value: The function returns the maximum number of elements that a deque c
1 min read
deque assign() function in C++ STLThe deque::assign() is a built-in function in C++ STL which is used to assign values to the same or different deque container. On being called more than once in the same program, the function destroys the values of the previous elements and re-assigns new set of elements to the container. Syntax: de
2 min read
deque rbegin() function in C++ STLThe deque::rbegin() is an inbuilt function in C++ STL which returns a reverse iterator which points to the last element of the deque (i.e., its reverse beginning). Syntax: deque_name.rbegin()Parameter: This function does not accept any parameters. Return value: It returns a reverse iterator which po
2 min read
deque rend() function in C++ STLThe deque::rend() is an inbuilt function in C++ STL which returns a reverse iterator which points to the position before the beginning of the deque (which is considered its reverse end). Syntax: deque_name.rend()Parameter: This function does not accept any parameters. Return value: It returns a reve
2 min read
deque cbegin() in C++ STLThe cbegin() method in deque is a function in C++ STL which returns an iterator pointing to the first element of the container. Syntax: deque_name.cbegin() Return value: It returns a constant iterator pointing to the first element of the deque. This means, that the iterator can be used to traverse t
2 min read
deque cbegin() in C++ STLThe cbegin() method in deque is a function in C++ STL which returns an iterator pointing to the first element of the container. Syntax: deque_name.cbegin() Return value: It returns a constant iterator pointing to the first element of the deque. This means, that the iterator can be used to traverse t
2 min read
deque::operator= and deque::operator[] in C++ STLDeque 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
4 min read
deque::at() and deque::swap() in C++ STLDeque 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
4 min read
How Deque Works Internally in C++? Prerequisite: Deque in C++ Deque or Double Ended Queue is a generalized version of the Queue data structure that allows insert and deletion at both ends. It supports the access of elements in O(1) time complexity and the insertion and deletion of elements from front and back are both done with O(1)
4 min read
Deque of Pairs in C++ with Examples What is a deque? In C++, the deque is a sequence container and it is also known by the name, double-ended queue. As the name implies, a deque allows insertion and deletion from both ends. Although a deque is similar to a vector, deques are more efficient compared to vectors. In vectors, contiguous s
5 min read
Difference between Queue and Deque in C++ Queue: A Queue is a linear data structure that follows a First In First Out (FIFO) order in which the operations are performed. It is a type of container adaptor where elements are inserted into one end of the container and deleted from the other. Functions: empty(): Tests whether the queue is empty
4 min read
Deque vs Vector in C++ STL Deque in C++ Standard Template Library (STL) Double-ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors but support inserting and deleting the first element in O(1). Unlike vectors, contiguous storage allocation is not guarante
2 min read
How to check/find an item in Dequeue using find() method find() function finds the element in the given range of numbers. Returns an iterator to the first element in the range [first, last) that compares equal to the value to be searched. If no such element is found, the function returns last. Syntax: InputIterator find (InputIterator first, InputIterator
6 min read