Sort b so you can binary search for it to reduce time complexity. Then use erase-delete idiom to discard all elements from a that are contained in b :
sort( begin(b), end(b) ); a.erase( remove_if( begin(a),end(a), [&](auto x){return binary_search(begin(b),end(b),x);}), end(a) );
Of course, you can sacrifice time complexity for simplicity and reduce your code by removing sort() and replacing binary_search() with find() :
a.erase( remove_if( begin(a),end(a), [&](auto x){return find(begin(b),end(b),x)!=end(b);}), end(a) );
This is a matter of taste. In both cases, you do not need heap allocations. By the way, I use automatic lambda parameters, which are C ++ 14. Some compilers already implement this function, such as clang. If you do not have such a compiler, but only C ++ 11, replace auto with the type of the container element.
By the way, this code does not mention any types! You can write a template function to make it work for all types. The first option requires random access iteration b , while the second part of the code does not.
Ralph Tandetzky Jan 18 '14 at 0:13 2014-01-18 00:13
source share