I want to pass a lambda function to for_each that captures a vector and delete all the elements that less than 10, it works partially but still showing elements that less than 10.
can anyone know what's wrong with it? the below code shows (10 8 5 6 4 77 15)
#include<iostream>
#include<vector>
#include<algorithm>
int main(){
std::vector<int>vec{10,3,8,2,5,9,6,7,4,1,77,15};
std::for_each(vec.begin(),vec.end(),[&vec](int x){if(x<10) vec.erase(remove(vec.begin(),vec.end(),x),vec.end());});
for(auto&i:vec)
std::cout<<i<<'\t';
return 0;
}