How to Access the Last Element in a Vector in C++? Last Updated : 16 Oct, 2024 Comments Improve Suggest changes Like Article Like Report 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 for accessing the last element of vector in C++:Table of ContentUsing vector::back() MethodUsing vector::size() MethodUsing vector::end() IteratorUsing vector::back() MethodThe simplest way to access the last element of a vector is by using the std::vector::back() member function. This function returns the last element of the vector.Example C++ // C++ program to access the last element of // a vector using std::back() #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {11, 23, 9, 7}; // Accessing the last element of the vector // with std::back() method int last = v.back(); cout << last; return 0; } Output7Time Complexity: O(1)Auxiliary Space : O(1)Using vector::size() MethodIn C++, std::vector is a zero indexed container, so the index of the last element is: size of the vector - 1. We can find size of the vector using std::vector::size() method. This function returns the size of the vector as integer.Example C++ // C++ program to show how to access the last // element of a vector using size()-1 #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {11, 23, 9, 7}; // Accessing the last element of the vector // using index int last = v[v.size() - 1]; cout << last; return 0; } Output7 Time Complexity: O(1)Auxiliary Space: O(1)We can also use the std::vector::at() method to access the last element using its index value.Using vector::end() IteratorWe can also access the last element of the vector end iterator which is returned by std::vector::end() method. The end iterator initially points to the theoretical element after the last element. We can decrement and dereference it to access the last element.Example C++ // C++ program to show how to access the last // element of a vector using iterator #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {11, 23, 9, 7}; // Iterator to the last element auto it = --v.end(); cout << *it; return 0; } Output7 Time Complexity: O(1)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Access the Last Element in a Vector in C++? anuragvbj79 Follow Improve Article Tags : C++ Programs C++ STL cpp-vector CPP Examples +1 More Practice Tags : CPPSTL Similar Reads How to Access Elements in a Vector in C++? In C++, vector provides fast access to its elements using their position as indexes. In this article, we will discuss different ways to access the element in vector using index in C++.The simplest method to access the elements of vector is by using vector operator [] with the corresponding index val 3 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 Access the First Element of a Vector in C++? In this article, we will learn how to access the first element of vector in C++.The most efficient way to access the first element of vector is by using vector front() function. Letâs take a look at a simple example:C++#include <bits/stdc++.h> using namespace std; int main() { vector<int 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 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 Add Elements in a Vector in C++? In C++, vector provides several built-in methods to insert the elements and efficiency of the insertion depends on the position where the insertion takes place. In this article, we will learn different ways to insert elements into a vector in C++ and also compare their efficiency.The simplest way to 3 min read How to Access Vector Element Using Iterator in C++? In C++, vectors are container that works like a dynamic array. STL provides iterators for the traversal of these containers. In this article, we will learn how to access an element in a vector using an iterator in C++. Example Input: myVector = {1, 5, 8, 1, 3, 4} Output: myVector value at index 5 us 2 min read How to Access First Element in a Vector Using Iterator in C++? In C++, vectors are containers similar to arrays but unlike arrays, vectors can resize themselves during the runtime. These vectors provide iterators to access their elements. In this article, we will learn how to access the first element in a vector using an iterator in C++. Example Input:myVector 2 min read How to Change an Element in a Vector in C++? In C++, vectors are dynamic containers that store the data in a contiguous memory location but unlike arrays, they can resize themselves to store more elements. In this article, we will learn how to change a specific element by its index in a vector in C++.The simplest way to modify an element is by 3 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 Like