I have two unordered_set and want the intersection of those. I can't find a library function to do that.
Essentially, what I want is this:
unordered_set<int> a = {1, 2, 3};
unordered_set<int> b = {2, 4, 1};
unordered_set<int> c = a.intersect(b); // Should be {1, 2}
I can do something like
unordered_set<int> c;
for (int element : a) {
if (b.count(element) > 0) {
c.insert(element);
}
}
However, I think there should be a more convenient way to do that. If there isn't one, can someone explain why? I know there is std::set_intersection
, but that seems to operate on vectors only.
unordered_set
. You sould use find instead of count though. @Creatinine set_intersection needs a sorted set. – Normand