How to Access the Last Element of a Deque in C++? Last Updated : 23 Jul, 2025 Comments Improve Suggest changes 1 Likes 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) Create Quiz Comment G gauravggeeksforgeeks Follow 1 Improve G gauravggeeksforgeeks Follow 1 Improve Article Tags : C++ Programs C++ STL cpp-deque CPP Examples +1 More Explore C++ BasicsIntroduction to C++3 min readData Types in C++6 min readVariables in C++4 min readOperators in C++9 min readBasic Input / Output in C++3 min readControl flow statements in Programming15+ min readLoops in C++7 min readFunctions in C++8 min readArrays in C++8 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++12 min readFile Handling in C++8 min readMultithreading in C++8 min readNamespace in C++5 min readOOP in C++Object Oriented Programming in C++8 min readInheritance in C++6 min readPolymorphism in C++5 min readEncapsulation in C++3 min readAbstraction in C++4 min readStandard Template Library(STL)Standard Template Library (STL) in C++3 min readContainers in C++ STL2 min readIterators in C++ STL10 min readC++ STL Algorithm Library3 min readPractice & ProblemsC++ Interview Questions and Answers1 min readC++ Programming Examples4 min read Like