As the accepted answer explains, there is nothing that directly does what you want, and std::set_intersection isn't intended for intersection std::unordered_sets, even though it sounds like it. The ideal solution depends on the C++ standard you're using.
Given two sets std::unordered_set<T> a, b; ...
In-place
... the best method to turn a into the intersection a & b is as follows:
// C++20 (needs <unordered_set>)
std::erase_if(a, [&b](int e) {
return !b.contains(e);
});
// C++11 (needs <unordered_set>)
for (auto it = a.begin(); it != a.end();) {
if (!b.count(*a)) { it = a.erase(it); }
else { ++it; }
}
Non-modifying
Alternatively, to compute a new set c which contains the intersection of a and b:
// C++23 (needs <unordered_set>, <ranges>)
std::unordered_set<int> c(std::from_range, a | std::views::filter([&b](int e) {
return b.contains(e);
});
// C++20 (needs <unordered_set>, <ranges>)
auto view = a | std::views::filter([&b](int e) {
return b.contains(e);
};
std::unordered_set<int> c(view.begin(), view.end());
// C++11 (needs <unordered_set>)
std::unordered_set<int> c;
// TODO: consider reserving a size (optimistically |a| + |b|)
for (int e : a) {
if (b.count(e)) { a.insert(e); }
}