📜  向量 c++ 代码示例之间的交集

📅  最后修改于: 2022-03-11 14:44:52.072000             🧑  作者: Mango

代码示例1
#include  //std::sort
#include  //std::cout
#include  //std::string
#include  //std::vector

std::vector intersection(std::vector &v1,
                                      std::vector &v2){
    std::vector v3;

    std::sort(v1.begin(), v1.end());
    std::sort(v2.begin(), v2.end());

    std::set_intersection(v1.begin(),v1.end(),
                          v2.begin(),v2.end(),
                          back_inserter(v3));
    return v3;
}

int main(){
    std::vector v1 {"a","b","c"};
    std::vector v2 {"b","c"};

    auto v3 = intersection(v1, v2);

    for(std::string n : v3)
        std::cout << n << ' ';
}