【C++】两个vector求交集

使用迭代器遍历v1,find函数查找v2中是否有相同的元素。

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>

using namespace std;

vector<string> intersection(vector<string> v1, vector<string> v2)
{
    vector<string> res_vec;
    for(auto iter=v1.begin(); iter!=v1.end(); ++iter)
    {
        if(find(v2.begin(), v2.end(), *iter)!=v2.end())
        {
            res_vec.emplace_back(*iter);
            //res_vec.push_back(*iter);
        }
    }
    return res_vec;
}

原文链接: https://www.cnblogs.com/from-zero/p/13275596.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/200143

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月12日 下午8:21
下一篇 2023年2月12日 下午8:21

相关推荐