How can i remove matched elements in c++ while keeping the same order ? i found similar question here but they sort the elements while i want to keep the order for example i have v1{1,2,3,4} , v2{8,6,2,4,3} ,the result should be {1},{8,6}.
here is my code
#include <iostream>
#include<vector>
using namespace std;
int main() {
vector<int> v1{1,2,3,4},v2{8,2,4,3};
for (auto i=0;i<v1.size();i++){
for (auto j=0;j<v2.size();j++){
if (v1[i]==v2[j])
{
v1.erase(v1.begin()+i);
v2.erase(v2.begin()+j);
}
}
}
return 0;
}
but it gives wrong result so how can i get it done?
If you can avoid raw loops, like this