相关疑难解决方法(0)

C++中的无序集合交集

这是我的代码,想知道任何想法让它更快?我的实现是蛮力,对于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)

c++ stl unordered-set

6
推荐指数
2
解决办法
3727
查看次数

标签 统计

c++ ×1

stl ×1

unordered-set ×1