Vector contains

Apr 25, 2011 at 7:30pm
I'm trying to look at 2 vectors of strings, we'll call them A and B. For each element in B, I want to check if that element is also in A. If that element is not in A, I want to insert it into an empty third vector called C.

This is what I have so far:

1
2
3
4
5
6
7
for(int i=0; i<B.size(); i++){
std::vector<string>::iterator it;
 it = std::find(A.begin(), A.end(), B[i]);
if (it == A.end()){
  C.push_back(B[i]);
}
}


I'm getting an error about a non-matching function call for the C.push_back() call. Can anyone see what I am doing wrong?
Apr 25, 2011 at 7:40pm
I don't see anything wrong with it...are you sure C is the same type as A/B?
Apr 25, 2011 at 11:44pm
closed account (zb0S216C)
It's worth mentioning the type-specifier of A, B, and C.
Apr 26, 2011 at 3:54am
I specifically replied to this thread... I wonder who deleted my response? And why?

If order doesn't matter, then sort A and B and use set_difference() in <algorithm>.

1
2
#include <algorithm>
#include <iterator> 
1
2
3
sort( A.begin(), A.end() );
sort( B.begin(), B.end() );
set_difference( B.begin(), B.end(), A.begin(), A.end(), back_inserter( C ) );

If order does matter, then post back and I'll give you something moderately more complex, but still useful.
Topic archived. No new replies allowed.