How to Delete Element from Specific Position in Vector? Last Updated : 16 Jan, 2025 Comments Improve Suggest changes Like Article Like Report In C++, vector provides access to its elements using their index values. In this article, we will learn how to delete an element from specific position in vector in C++.The recommended method to delete an element from specific position in vector is by using vector erase() method. Let’s take a look at an example: C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 2, 3, 4, 5}; // Remove the 2nd index element 6 v.erase(v.begin() + 2); for (auto i : v) cout << i << " "; return 0; } Output1 2 4 5 Explanation: The vector erase() method deletes the element using the iterator to it. The iterator can be found by adding the index to the vector begin() method.There are also some other methods in C++ to delete an element from specific position in vector. Some of them are as follows:Table of ContentUsing remove() with Vector erase()Using copy() with Vector resize()Manually Using LoopUsing remove() with Vector erase()The remove() function shifts all the elements after the given index one place to the left and returns the iterator to the new end of the range which only contains the elements that are to be deleted. We can then remove the extra elements using vector erase(). C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 2, 3, 4, 5}; // remove the element at index 2 v.erase(remove(v.begin(), v.end(), v[2]), v.end()); for (int i : v) cout << i << " "; return 0; } Output1 2 4 5 Using copy() with Vector resize()The program removes an element from a specific index in the vector by shifting all elements after that index one position to the left using the copy() function. Then, the vector size is decreased by 1 using vector resize() to remove the last element. C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 2, 3, 4, 5};; // Copy elements after index 2 one place to left copy(v.begin() + 3, v.end(), v.begin() + 2); // Resize the vector v.resize(v.size() - 1); for (auto i : v) cout << i << " "; return 0; } Output1 2 4 5 Manually Using LoopThis is basically the internal implementation of vector erase() method. All elements after the given index are manually shifted one position to the left using a loop. Then, the vector pop_back() function is called to remove the last element to remove it. C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 2, 3, 4, 5}; // Shifting the element after index 2 for (int i = 3; i < v.size(); i++) v[i - 1] = v[i]; // Removing the last element v.pop_back(); for (auto i : v) cout << i << " "; return 0; } Output1 2 4 5 Comment More infoAdvertise with us Next Article How to Delete Element from Specific Position in Vector? anuragvbj79 Follow Improve Article Tags : C++ Programs C++ STL cpp-vector CPP Examples +1 More Practice Tags : CPPSTL Similar Reads How to Delete Multiple Elements from a Vector in C++? In this article, we will learn how to erase multiple elements from a vector in C++.The easiest and most efficient method to delete the multiple elements from the vector is by using vector erase() method. Letâs take a look at an example:C++#include <bits/stdc++.h> using namespace std; int main( 2 min read How to Delete All Elements from a Vector in C++? In C++, you can delete all items from a vector to either clear its contents or free its memory. In this article, we will learn how to delete all items from a vector in C++.The recommended way to delete all items from a vector is by using the vector clear() function. Letâs take a look at a simple exa 2 min read How to Remove Last Element from Vector in C++? 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() { 2 min read How to Delete an Element from a Multiset in C++? In C++, a multiset is a container that stores elements in a specific order. Multiple elements can have the same values. In this article, we will learn how to delete a specific element from a multiset. Example: Input: myMultiset = {5, 2, 8, 5, 8, 8}; Element to delete: 8 Output: myMultiset = {5, 2, 5 2 min read How to Delete Multiple Elements from a Set in C++? In C++, sets are containers that store unique elements in some sorted order. In this article, we will learn how to delete multiple elements from a set using C++. Example: Input: set = {10, 20, 30, 40, 50, 60}elements_to_delete = {20, 30, 40}Output: Elements of set after deletion: 10 50 60Removing Mu 2 min read How to Delete an Element from a Set in C++? A set in C++ is a container that stores unique elements in a sorted order. In this article, we will learn how to delete a specific element from a set. Example: Input: mySet = {5, 2, 8, 1, 4} Element to delete: 2 Output: mySet = {5, 1, 8, 4}Delete an Element from a Set in C++To delete a specific elem 2 min read How to Delete the Last Element from a Multiset in C++? In C++, a multiset is a container that can store multiple elements, including duplicates. In this article, we will learn how to delete the last element from a multiset in C++. Example: Input:myMultiset = { 1, 2, 2, 3, 3, 3, 4, 4, 4, 4 }Element to delete = 4Output: myMultiset = { 1, 2, 2, 3, 3, 3, 4, 2 min read 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 Delete an Element from a Priority Queue in C++ ? In C++, a priority queue is a queue in which elements are arranged based on their priority values as each element has a priority value associated with it. In this article, we will learn how to delete an element from a priority queue in C++. For Example, Input: myPriorityQueue = {8, 6, 7, 5, 4, 2, 3} 2 min read How to Insert an Element at a Specific Index in a Vector in C++? In C++, vector allows access to its elements using indexes. In this article, we will learn how to insert an element at a specific index in a vector in C++.The simplest method to insert an element at a specific index in a vector is by using vector insert() method. Letâs take a look at a simple exampl 3 min read Like