这是我的代码,想知道任何想法让它更快?我的实现是蛮力,对于a中的任何元素,尝试查找它是否也在b中,如果是,则放入结果集c.任何更聪明的想法都表示赞赏.
#include <iostream>
#include <unordered_set>
int main() {
std::unordered_set<int> a = {1,2,3,4,5};
std::unordered_set<int> b = {3,4,5,6,7};
std::unordered_set<int> c;
for (auto i = a.begin(); i != a.end(); i++) {
if (b.find(*i) != b.end()) c.insert(*i);
}
for (int v : c) {
std::printf("%d \n", v);
}
}
Run Code Online (Sandbox Code Playgroud)