For example, I good 2 vector, say vector current, vector to_delete. Is there some good way to delete elements which appear both in current.
Here is my way to do it. I guess it not looks good. So please help me out. Thank you.
#include <iostream>
#include <vector>
using namespace std;
int main(){
vector <double> current;
current.push_back(1.1);
current.push_back(2.1);
current.push_back(3.1);
current.push_back(4.1);
current.push_back(5.1);
current.push_back(6.1);
current.push_back(7.1);
current.push_back(8.1);
vector <double> to_delete;
to_delete.push_back(2.1);
to_delete.push_back(5.1);
for(int i = 0;i<to_delete.size();i++){
int loc = -1;
for(int j = 0; j < current.size();j++)
{
if(current[j]==to_delete[i]){
loc = j;
break;
}
}
if(loc >= 0){
current.erase(current.begin()+loc);
}
}
for(int i = 0;i < current.size();i++){
cout << current[i]<< endl;
}
}
You could use
std::find()instead of a manual loop to find the position of the element that should be deleted.