Hi,
I am trying to compare 2 vectors to remove any kind of overlap. I have written a simple program but it gives a segmentation fault once I increase / decrease number of vector elements in both.
Is there a better way of doing this. Especially suppose one of the vector is empty or both are empty my program won't work. Can anybody help.
Here is my program:-
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main(){
vector<string> v1;
vector<string> v2;
v1.push_back("A");
v1.push_back("B");
//v1.push_back("C");
//v1.push_back("D");
v2.push_back("A");
v2.push_back("B");
v2.push_back("C");
v2.push_back("D");
//v2.push_back("E");
//v2.push_back("F");
//v2.push_back("G");
vector<string>::iterator iter = v2.begin();
while( iter != v2.end() )
{
for(int i=0; i<v1.size(); i++){
if (*iter == v1[i]){
iter = v2.erase( iter );
}
else{
++iter;
}
}
}
for(int i=0; i<v2.size(); i++){
cout << v2[i];
cout << endl;
}
}
In this program output is correct:-
C
D
but if I increase or decrease elements the problem starts and gives segmentation faults.
Thanks