How to Access the Last Element in a Vector in C++?
Last Updated :
16 Oct, 2024
Given a vector of n elements, the task is to access the last element in C++.
Examples
Input: v = {11, 23, 9, 7};
Output: 7
Explanation: Since 7 is the last element of the vector.
Input: v = {1, 3, 11, 52};
Output: 52
Explanation: Since 52 is the last element of the vector.
Following are the different ways for accessing the last element of vector in C++:
Using vector::back() Method
The 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;
}
Time Complexity: O(1)
Auxiliary Space : O(1)
Using vector::size() Method
In 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;
}
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() Iterator
We 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;
}
Time Complexity: O(1)
Auxiliary Space: O(1)
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