Remove Elements that are in one vector from another

I need to remove the elements that appear in Vector A and Vector B, but keep the elements that are only in Vector A. The vectors can be of any size, but are not necessarily equal to each other.

For example, if:
vector A contains the values <1,4,66,22>
vector B contains the values <1,22,44,93,102,543>

Then after preforming the operation:
vector A should contain <4,66>
vector B should contain <44,93,102,543>

Do I need to loop through both with a for loop and strncmp the values or is the a function that I can use to streamline the process?
Last edited on
If you could sort both vectors, then you could compute their intersection
http://www.cplusplus.com/reference/algorithm/set_intersection/
and finally remove the intersection from each vector with std::set_difference

The set_intersection shows its possible implementation. You could build from that code a function that takes two sorted ranges and returns two ranges: uniques of each input range.


PS. std::string has operator== so strncmp() is not necessary.
Topic archived. No new replies allowed.