Nikos Athanasiou - 9 months ago 69

C++ Question

The set_difference algorithm requires the following

The elements in the ranges shall already be ordered according to this same criterion

which is not the case for hash tables.

I'm thinking of implementing a set difference A-B in terms of

`std::remove_copy`

Is there a standard-valid-fastest-safest way to do it?

Answer Source

If you have two hash tables, the most efficient way should be to iterate over one of them, looking up each element in the other hash table. Then insert the ones you do not find into some third container. A rough sketch might look like this:

```
std::vector<int> result;
std::copy_if(lhs.begin(), lhs.end(), std::back_inserter(result),
[&rhs] (int needle) { return rhs.find(needle) == rhs.end(); });
```