且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

仅使用重复项从其他人创建新载体

更新时间:2023-12-01 10:31:46

如上所述,您可以使用算法 set_intersection 即可:
但是您还必须对 vector 第一个

As mentioned, you can use an algorithm set_intersection to do this: But you will also have to sort the vectors first

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
int main()
{
    std::vector<int> a = {0,1,2,3,4,5,6,7,8,9,10};
    std::vector<int> b = {0,2,4,6,8,10};
    std::vector<int> c = {0,1,2,4,5,8};

    std::vector<int> temp;
    std::vector<int> abc;

    std::sort(a.begin(), a.end());
    std::sort(b.begin(), b.end());
    std::sort(c.begin(), c.end());

    std::set_intersection(a.begin(), a.end(),
                          b.begin(), b.end(),
                          std::back_inserter(temp));

    std::set_intersection(temp.begin(), temp.end(),
                          c.begin(), c.end(),
                          std::back_inserter(abc));

    for(int n : abc)
        std::cout << n << ' ';
}

实时演示