How to Access the Last Element of a List in C++? Last Updated : 15 Apr, 2024 Comments Improve Suggest changes Like Article Like Report 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 Element in a List in C++ The simplest and most efficient way to access the last element of a std::list is by using the std::list::back() member function that returns a reference to the last element in the list. Syntax to Use std::list::back() FunctionlistName.back()C++ Program to Access the Last Element in a List The below example demonstrate the use of the std::list::back() function to access the last element of a std::list in C++ STL. C++ // C++ program demonstrate the use of the std::list::back() // function to access the last element of a std::list #include <iostream> #include <list> using namespace std; int main() { // Creating a list of integers list<int> nums = { 10, 20, 80, 90, 50 }; // Accessing the last element int lastElement = nums.back(); // Printing the last element cout << "Last element of the list is : " << lastElement << endl; return 0; } OutputLast element of the list is : 50 Time Complexity: O(1)Auxiliary Space: O(1) In the above example, myList.back() returns a reference to the last element in the list myList, and we store it in the variable lastElement. Then, we simply print out the value of lastElement. Note: We can also use std::list::end() function that returns an iterator pointing to the position after the last element in the list, and then decrement the iterator to point to the last element. Comment More infoAdvertise with us Next Article How to Access the Last Element of a List in C++? R rohitpant4532 Follow Improve Article Tags : C++ Programs C++ STL cpp-list cpp-list-functions CPP Examples +2 More Practice Tags : CPPSTL Similar Reads How to Access the Last Element of a Deque in C++? 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 Eleme 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 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 the Top Element of a Stack in C++? In C++, The stack is a container adapter that provides a Last-In-First-Out (LIFO) type of data structure in which insertion and deletion are done at the end only. In this article, we will learn how to access the top element of a stack in C++. Example: Input: myStack = {10, 20, 30, 40} Output: Top El 2 min read How to Add an Element at the End of List in C++? In C++, the Standard Template Library (STL) has a doubly-linked list container, known as std::list that offers various functions to manipulate elements. In this article, we will learn how to add an element at the end of a list in C++ STL. Example: Input: myList = {1, 2, 3}; Output: List after Elemen 2 min read Like