The standard way of intersecting two sets in C++ is to do the following:
std::set<int> set_1; // With some elements
std::set<int> set_2; // With some other elements
std::set<int> the_intersection; // Destination of intersect
std::set_intersection(set_1.begin(), set_1.end(), set_2.begin(), set_2.end(), std::inserter(the_intersection, the_intersection.end()));
How would I go about doing an in-place set intersection? That is, I want set_1 to have the results of the call to set_intersection. Obviously, I can just do a set_1.swap(the_intersection)
, but this is a lot less efficient than intersecting in-place.
I think I've got it:
Anyone see any problems? Seems to be O(n) on the size of the two sets. According to cplusplus.com, std::set erase(position) is amortized constant while erase(first,last) is O(log n).
You can easily go through
set_1
, check each element to see if it exists inset_2
, and erase it if it doesn't. Since sets are sorted, you can compare them in linear time, and erasing an element using an iterator is amortized constant time. I wouldn't count on it being more efficient than what you started with though, benchmarking would be wise if it matters to you.It's not directly answers the question, but maybe someone find this helpful.
In case of
std::vector
it is safe to use standard algorithm withset_1.begin()
as output iterator. Note,set_2
could be anything, not just astd::vector
.