How to Access the Last Element of a Deque in C++? Last Updated : 01 Apr, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In C++ STL, we have a deque container which is a double-ended queue that allows us to add or remove elements from both ends. In this article, we will learn how to access the last element in a deque in C++. For Example, Input:myDeque = {1, 2, 3, 4, 5, 6}Output: Last Element: 6Accessing the Last Element in a Deque in C++ To access the last element of a std::deque in C++, we can use the std::deque::back() member function which returns a reference to the last element of the deque. This function is suitable for accessing and manipulating the last element without modifying the deque's structure. Syntax to Use std::backdequeName.back();C++ Program to Access the Last Element of a DequeThe below program demonstrates how we can access the last element of a deque in C++. C++ // C++ program to demonstrate how to access the last element // of a deque #include <deque> #include <iostream> using namespace std; int main() { // Initializing a new deque deque<int> myDeque = { 1, 2, 3, 4, 5 }; // Using back() function to retrieve the last element int lastElement = myDeque.back(); // Printing the last element cout << "The last element is: " << lastElement << endl; return 0; } OutputThe last element is: 5 Time Complexity: O(1)Auxilliary Space: O(1) Comment More infoAdvertise with us Next Article How to Access the Last Element of a Deque in C++? G gauravggeeksforgeeks Follow Improve Article Tags : C++ Programs C++ STL cpp-deque CPP Examples +1 More Practice Tags : CPPSTL Similar Reads How to Access the First Element of a Deque in C++? In C++, deques also called double-ended queues are sequence containers that can perform insertions and deletions on both ends. In this article, we will learn how to access the first element of a deque in C++. Example: Input: myDeque = {1,2,3,4,5} Output: Deque Elements: 1 2 3 4 5 First Element: 1Acc 2 min read How to Access the Last Element of a List in C++? In C++STL, the list container is a doubly-linked list that stores elements in non-contiguous memory locations. In this article, we will learn how to access the last element in a list in C++. For Example, Input: myList = {10, 20, 80, 90, 50}; Output: Last element of the list is : 50Access the Last El 2 min read How to Access the Last Element in a Vector in C++? Given a vector of n elements, the task is to access the last element in C++.ExamplesInput: v = {11, 23, 9, 7};Output: 7Explanation: Since 7 is the last element of the vector.Input: v = {1, 3, 11, 52};Output: 52Explanation: Since 52 is the last element of the vector.Following are the different ways f 2 min read How to Access First Element of a List in C++? In C++, a list is a sequential container that allows constant time insertions and deletions and allows the users to store data in non-contiguous memory locations. In this article, we will learn how to access the first element in the list in C++. Examples: Input: myList ={1, 2, 3, 4, 5} Output: The f 2 min read How to Add an Element at the End of a Deque in C++? In C++, a deque is a flexible data structure that supports the insertion and deletion of elements from both ends efficiently. In this article, we will learn how to add an element at the end of a deque in C++. Example: Input: myDeque ={10,20,30,40} Output: // added element 50 at the end myDeque ={10, 2 min read Like