C++ 查找两个排序向量中的公共元素

C++ 查找两个排序向量中的公共元素,c++,vector,C++,Vector,我需要写一个函数,它接受向量的常量引用,并返回一个升序向量,即数字 我有两个整数的排序向量作为参数,只需要使用标题查找所有公共元素 有什么想法吗?我想不出来。因为元素是有序的,所以只需将它们传递一次 对于每个迭代器,如果两个范围都不在末尾,则继续 如果两个元素都不小于另一个,则它们相等,可以将其中一个写入结果 否则,您需要推进指向较小元素的迭代器 请参阅可能的实现 模板 输出集\u交叉点(输入第一个1,输入最后一个1, InputIt2 first2,InputIt2 last2, 输出(第一)

我需要写一个函数,它接受向量的常量引用,并返回一个升序向量,即数字

我有两个整数的排序向量作为参数,只需要使用
标题查找所有公共元素


有什么想法吗?我想不出来。

因为元素是有序的,所以只需将它们传递一次

对于每个迭代器,如果两个范围都不在末尾,则继续

如果两个元素都不小于另一个,则它们相等,可以将其中一个写入结果

否则,您需要推进指向较小元素的迭代器

请参阅可能的实现

模板
输出集\u交叉点(输入第一个1,输入最后一个1,
InputIt2 first2,InputIt2 last2,
输出(第一)
{
while(first1!=last1&&first2!=last2){
如果(*first1<*first2){
++第一个1;
}否则{
如果(!(*first2<*first1)){
*d_first++=*first1++;
}
++前2名;
}
}
首先返回d_;
}
让我们将其调整为“除了
”规则

#包括
样板
标准::向量集_相交(常数标准::向量和一,常数标准::向量和二)
{
std::向量结果;
std::vector const_迭代器first1=1.begin(),last1=1.end(),first2=2.begin(),last2=2.end();
while(first1!=last1&&first2!=last2){
如果(*first1<*first2){
++第一个1;
}否则{
如果(!(*first2<*first1)){
结果。向后推_(*first1++);
}
++前2名;
}
}
返回结果;
}

尽管Caleths的答案绝对有效,但您可能是一名初学者,并且可能需要一种更简单的方法(用于学习语言等)

这将是:

std::vector<int> find_same_ints(const std::vector<int> &vec1, const std::vector<int> &vec2)
{
    std::vector<int> results;

    //Check if same number exists in the both vector arguments, if so, push the number to results
    for(int x : vec1)
        for(int y : vec2)
            if(x == y)
                results.push_back(x);

    //Simple bubblesort algorithm
    for(std::size_t x = 0; x < results.size(); x++)
    {
        int lowestValueIndex = x;

        //Try to find the lowest value of the remaining not sorted values
        for(std::size_t y = x + 1; y < results.size(); y++)
            if(results[y] < results[lowestValueIndex])
                lowestValueIndex = y;

        //Swap the values
        int temp = results[lowestValueIndex];
        results[lowestValueIndex] = results[x];
        results[x] = temp;
    }

    //Return sorted vector
    return results;
}
std::vector find_same_int(常数std::vector&vec1,常数std::vector&vec2)
{
std::矢量结果;
//检查两个向量参数中是否存在相同的数字,如果存在,则将该数字推送到结果
for(int x:vec1)
for(int y:vec2)
如果(x==y)
结果:推回(x);
//简单气泡排序算法
对于(std::size_t x=0;x

请记住,Caleths算法是您在获得该语言的经验并理解代码中实际发生的情况时应该使用的算法。

仅使用标题?这是什么意思?在
中,您可以找到对大多数部分执行此操作的
set_intersection
。不能使用算法,只能使用头向量。您是说您的函数应该将两个整数的排序向量合并为一个排序向量吗?不,函数应该返回只包含公共元素的排序向量。您是在问吗“我如何实现
设置交叉点
”?好的!非常感谢:)是的!我是初学者,我听到了算法非常重要的观点。我会记住这一点。:)
#include <vector>

template <typename T>
std::vector<T> set_intersection(const std::vector<T> & one, const std::vector<T> & two)
{
    std::vector<T> result;

    std::vector<T> const_iterator first1 = one.begin(), last1 = one.end(), first2 = two.begin(), last2 = two.end();

    while (first1 != last1 && first2 != last2) {
        if (*first1 < *first2) {
            ++first1;
        } else  {
            if (!(*first2 < *first1)) {
                result.push_back(*first1++);
            }
            ++first2;
        }
    }

    return result;
}
std::vector<int> find_same_ints(const std::vector<int> &vec1, const std::vector<int> &vec2)
{
    std::vector<int> results;

    //Check if same number exists in the both vector arguments, if so, push the number to results
    for(int x : vec1)
        for(int y : vec2)
            if(x == y)
                results.push_back(x);

    //Simple bubblesort algorithm
    for(std::size_t x = 0; x < results.size(); x++)
    {
        int lowestValueIndex = x;

        //Try to find the lowest value of the remaining not sorted values
        for(std::size_t y = x + 1; y < results.size(); y++)
            if(results[y] < results[lowestValueIndex])
                lowestValueIndex = y;

        //Swap the values
        int temp = results[lowestValueIndex];
        results[lowestValueIndex] = results[x];
        results[x] = temp;
    }

    //Return sorted vector
    return results;
}