how to remove element in vector c++ from front code example
Example 1: how to remove an element from a vector by value c++
std::vector<int> v;
v.erase(std::remove(v.begin(), v.end(), 99), v.end());
Example 2: remove element by value vector c++
#include<bits/stdc++.h>
using namespace std;
int main(){
vector<int> v;
v.push_back(20);
v.push_back(10);
v.push_back(30);
v.push_back(20);
v.push_back(40);
v.push_back(20);
v.push_back(10);
vector<int>::iterator new_end;
new_end = remove(v.begin(), v.end(), 20);
for(int i=0;i<v.size(); i++){
cout << v[i] << " ";
}
return 0;
}
C++Copy
Example 3: vector erase specific element
template <typename T>
void remove(std::vector<T>& vec, size_t pos)
{
std::vector<T>::iterator it = vec.begin();
std::advance(it, pos);
vec.erase(it);
}