How to Delete Element from Specific Position in Vector?
Last Updated :
16 Jan, 2025
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;
}
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:
Using 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;
}
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;
}
Manually Using Loop
This 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;
}
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