1// Deleting first element
2vector_name.erase(vector_name.begin());
3
4// Deleting xth element from start
5vector_name.erase(vector_name.begin()+(x-1));
6
7// Deleting from the last
8vector_name.pop_back();
1vector.erase(position) // remove certain position
2// or
3vector.erase(left,right) // remove positions within range
4
1#include<bits/stdc++.h>
2using namespace std;
3int main(){
4 vector<int> v;
5 //Insert values 1 to 10
6 v.push_back(20);
7 v.push_back(10);
8 v.push_back(30);
9 v.push_back(20);
10 v.push_back(40);
11 v.push_back(20);
12 v.push_back(10);
13
14 vector<int>::iterator new_end;
15 new_end = remove(v.begin(), v.end(), 20);
16
17 for(int i=0;i<v.size(); i++){
18 cout << v[i] << " ";
19 }
20 //Prints [10 30 40 10]
21 return 0;
22}
23C++Copy
1template <typename T>
2void remove(std::vector<T>& vec, size_t pos)
3{
4 std::vector<T>::iterator it = vec.begin();
5 std::advance(it, pos);
6 vec.erase(it);
7}