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

1) In my version I'd use a third vector to hold the results which eleminates to erase from either of the two source vectors, but you can do it as you please. Are duplicates allowed in either of the source vectors or the result vector?
2) use two iterators, one to traverse each vector each vector.
3) check each vector to see if either is empty before entering the following loop
4) use a nested loop so that you compare element of vec1 with each element of vec2. If any two elements are equal then don't add the current vec1 element to the result vector. This will look for elements in vec1 not in vec2
5) then do the loops over again comparing each element of vec2 with each element of vec1. This will look for elements in vec2 not in vec1
6) at the end of the looping you can display the result vector.

nvm, had a brain fart...:P

1) In my version I'd use a third vector to hold the results which eleminates to erase from either of the two source vectors, but you can do it as you please. Are duplicates allowed in either of the source vectors or the result vector?
2) use two iterators, one to traverse each vector each vector.
3) check each vector to see if either is empty before entering the following loop
4) use a nested loop so that you compare element of vec1 with each element of vec2. If any two elements are equal then don't add the current vec1 element to the result vector. This will look for elements in vec1 not in vec2
5) then do the loops over again comparing each element of vec2 with each element of vec1. This will look for elements in vec2 not in vec1
6) at the end of the looping you can display the result vector.

Hi duplicates are not allowed in my case:-

I tried something like this. Near the solution i guess ;-)
I want only

E
F
G
H

to be printed but its printing

B
C
D
E
F
G

How can I get that ?

#include<iostream>
#include<string>
#include<vector>
using namespace std;

int main(){

           vector<string> v1;
           vector<string> v2;
           vector<string>::iterator iter1;
           vector<string>::iterator iter2;		   

           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");


  // assign elements in vector a value
  iter1 = v1.begin();
  iter2 = v2.begin();
  
  int i = 0;
  int j = 0;
  while(iter1 != v1.end()) {
   while(iter2 != v2.end()){
    
	 if(*iter2 != *iter1){
	     if(v1.size() > v2.size()){
	        cout << *iter1 << endl;
		 } else {
		    cout << *iter2 << endl;
		 }
	 }
	
    ++iter2;
    j++;
   }	
    ++iter1;
    i++;   
  }

     for(int i=0; i<v2.size(); i++){
          //cout << v2[i];
          //cout << endl;
     }

}

Thanks

Not sure where the H came from in the expected output since neither vector originally had an H in it, but that was probably a typo.

//check if elements of v1 are in v2
bool found = false;
for(iter1 = v1.begin(); iter1 != v1.end(); ++iter1)
{
   for(iter2 = v2.begin(); iter2 != v2.end(); ++iter2)
   {
      if(*iter1 == *iter2)
      {
        found = true;
        break;
      }
   }
   if(found)
     found = false;
   else //this element of v1 isn't in v2, so put it in the result vector
     resultVector.push_back(*iter1);
}

Also as most times, there is a function for this in algorithm library, in the algorithm header..

Thanks guys. Appreciate your help.:P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.