How to Remove Last Element from Vector in C++? Last Updated : 19 Nov, 2024 Comments Improve Suggest changes Like Article Like Report Given a vector of n elements, the task is to remove the last element from the vector in C++.The most efficient method to remove the last element from vector is by using vector pop_back() method. Let’s take a look at a simple example: C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 5, 7, 9}; // Remove the last element 9 v.pop_back(); for (auto i : v) cout << i << " "; return 0; } Output1 5 7 Explanation: The vector pop_back() function remove the last element from vector directly and does not return any value.There are also some other methods in C++ by which we can remove the last element from vector. Some of them are as follows:Table of ContentUsing vector erase()Using vector resize() Using vector erase()The vector erase() function can also be used to remove the last element from vector by specifying the position. C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 5, 7, 9}; // Remove the last element 9 v.erase(--v.end()); for (auto i : v) cout << i << " "; return 0; } Output1 5 7 Explanation: In this method, we provide an iterator to the last element of vector and it will remove an element of given position.Using vector resize() The vector resize() method is used to resize the size of vector. If we resize the vector to the size less that the current size, the extra elements are deleted from the end. So, by reducing the size by 1, we can delete the last element from the vector. C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 5, 7, 9}; // Remove the last element 9 v.resize(v.size() - 1); for (auto i : v) cout << i << " "; return 0; } Output1 5 7 Comment More infoAdvertise with us Next Article How to Remove Last Element from 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 Remove an Element from Vector in C++? In this article, we will learn how to remove a given element from the vector in C++.The most straightforward method to delete a particular element from a vector is to use the vector erase() method. Let's look at a simple example that shows how to use this function:C++#include <bits/stdc++.h> u 2 min read How to Remove an Element from a List in C++? In C++, the STL provides a std::list container that represents a doubly linked list to store the sequential data in non-contiguous memory locations. In this article, we will learn how to remove an element from a list in C++. Example: Input: myList = {1, 2, 3, 4, 5, 6, 7, 8} Target = 5 Output: // rem 2 min read How to Remove Last Occurrence of an Element from a Vector in C++? In C++, vectors are the same as dynamic arrays with the ability to resize automatically when an element is inserted or deleted, with their storage being handled automatically by the container. In this article, we will learn how to remove the last occurrence of a specific element in a vector. Input: 2 min read How to Remove Elements from a Vector while Iterating in C++? Erasing elements from a vector while iterating can be challenging because removing an element may invalidate iterators or change the size of the vector. In this article, we will learn how to erase an elements from vector while iterating in C++.To efficiently remove elements from a vector while itera 2 min read How to Remove an Element from the End of a List in C++? In C++, lists are data structures that allow us to store data of the same type in non-contiguous memory locations. In this article, we will learn how to remove an element from the end of a list in C++. Example Input: myList={10,20,30,40,50} Output: List Elements: 10 20 30 40Delete the Last Element f 2 min read Like