I have 2 vectors vc
and v2
I want to remove all elements from vc
that are contained in v2.
I try to do this by 2 nested loops. However the compiler gives an error: Debug Assertion Failed
. I would like to ask why is that and how can I fix it?
Thanks in advance!
#include <iostream>
#include <vector>
#include <string>
using namespace std;
vector <string> vc;
vector <string> v2;
int main()
{
vc.push_back("ala");
vc.push_back("bala");
vc.push_back("test");
vc.push_back("sample");
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
v2.push_back("test");
v2.push_back("bala");
for (auto i = vc.begin(); i != vc.end(); i++) {
for (auto j = v2.begin(); j != v2.end(); j++) {
if (i == j) {
vc.erase(i);
}
}
}
//it should print only ala and sample after the removal process, but it gives
//debug assertion error
for (int i = 0; i < vc.size(); i++) {
cout << vc[i] << endl;
}
}