I can't infer I can use std::set_difference from documentation, because it says sets should be ordered, which means they are not sets, but lists. Also all examples are about ordered lists, not sets.
How to know the truth?
std::set_difference
is for use with arbitrary sorted inputs (pre-sorted std::vector
s, std::list
s, std::deque
s, plain array, etc.), it just happens to work with std::set
(which is sorted) too.
If you're working with std::unordered_set
(or std::set
, and you're okay with operating in place), you'd just use the erase
method to remove all elements from one such set from another to get the difference, e.g.:
for (const auto& elem : set_to_remove) {
myset.erase(elem);
}
You can also do it into a new set with std::copy_if
; the recipe there is trivially adaptable to the case of symmetric difference (it's just two calls to std::copy_if
, where each one runs on one input set, and is conditioned on the element not existing in other input set).