Deque::back
deque::back is used to return a reference to the last element in the container. It returns the direct reference of the element.
Syntax:
deque_name.back()
Parameters: It does not take any parameters
Return Value: Deque::back accepts an Integer type return value
Iterator Validity: In this container, all the iterators, pointers, and references are invalidated.
Header File:
<deque>
Exceptions: If the container is empty then it will cause undefined behavior.
Example:
C++
// C++ program to demonstrate deque::back #include <deque> #include <iostream> using namespace std; // Driver code int main() { // Declaration of Deque deque< int > GFG = { 10, 20, 30, 40, 50, 60 }; int last_element = GFG.back(); cout << "Last ELement is : " << last_element << endl; return 0; } |
Output -:
Last ELement is : 60
Time Complexity: O(1)
Space Complexity: O(1)
deque::assign vs deque::back
Below are the differences between deque::assign and deque::back
Basis | deque::assign | deque::back |
---|---|---|
Definition | It is used to assign new contents to the deque container, replacing its current contents. | It is used to return a reference to the last element in the container. |
Syntax | dequename.assign(<int> size, <int> val); | dequename.back() |
No of Parameters | It takes two parameters | It does not take any parameters. |
Return Value | It does not have any return value. | Its return value is of Integer type |
Complexity | Linear | Constant |
Difference Between deque::assign and deque::back in C++
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 the case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed.