简体   繁体   English

找到两个向量之间的交点

[英]Finding the Intersection between two Vectors

And I'm doing pretty good in fact I found the intersection and think I have the right code. 而且我做得非常好,事实上我找到了交叉点,并认为我有正确的代码。 Only problem is that it doesn't seem to print out the last value. 唯一的问题是它似乎没有打印出最后一个值。

So if I have two sets: 所以如果我有两套:

9 12 7 8 1 19 11 2 14

15 10 8 2 5 16 14 7 19 0 11 3 13 18 9 17 1 12

My code will produce the following output: 我的代码将产生以下输出:

1
2
7
8
9
11
12
14

But the right intersection of the sets should be: 但是这些集合的正确交集应该是:

1
2
7
8
9
11
12
14
19

So, my code doesn't print out the last value and I can't find out why. 所以,我的代码不打印出最后一个值,我找不到原因。

void findIntersection(vector<int> A, vector<int> B)
{
    vector<int> intersection;
    int n1 = A.size();
    int n2 = B.size();
    int i = 0, j =0;
    while(i <= n1 && j <= n2)
    {
        if(A[i] > B[j])
        {
            j++;
        }
        else if( B[j] > A[i])
        {
            i++;
        }
        else
        {
            intersection.push_back(A[i]);
            i++;
            j++;
        }
    }

    for(unsigned int i = 0; i <= intersection.size(); i++)
    {
        cout << intersection[i] << endl;
    }
}

void slowintersect(vector<vector<int> > v)
{
    vector<int> vec;
    vector<int> c;
    int store_0;

    int row = 0;
    for(size_t j =0; j < v.at(row).size(); j++)
    {
        store_0 = v[row][j];
        c.push_back(store_0);
    }

    for(size_t i = 0; i < v.size(); i++)
    {
        for(size_t k = 0; k < v.at(i).size(); k++)
        {
            vec.push_back(k);
        }
    }

    findIntersection(c, vec);
}
#include <algorithm>
#include <set>

set<int> intersect;
set_intersection(s1.begin(),s1.end(),s2.begin(),s2.end(), // sorted!
                  std::inserter(intersect,intersect.begin()));

example

if you don't want std::algorithm , there are several errors in your code. 如果你不想要std::algorithm ,你的代码中会有几个错误。 ie: 即:

for(unsigned int i = 0; i <= intersection.size(); i++)
                           ^
                        should be <

here is an important error: 这是一个重要的错误:

int i = 0, j =0;
while(i <= n1 && j <= n2)
{        ^          ^
         <          <
    if(A[i] > B[j])
    {
        j++;

you will skip all n-1 A elements if A[0] is greater than each B element, and end the loop 如果A [0]大于每个B元素,则跳过所有n-1个A元素,并结束循环

I'm assuming you do not want to use the C++ standard algorithm. 我假设你不想使用C ++标准算法。

Couple of things. 几件事。
1. Your algorithm shall work only if both vectors are initially sorted. 1.只有在最初对两个向量进行排序时,您的算法才有效。 Are they? 是吗?
2. You shouldn't access vector[vector.size()] element as it is out of bounds. 2.您不应该访问vector [vector.size()]元素,因为它超出了界限。

My second point means: 我的第二点意味着:

while(i <= n1 && j <= n2)

Change this to 将此更改为

while(i < n1 && j < n2)

And change the following 并改变以下内容

for(unsigned int i = 0; i <= intersection.size(); i++)

to

for(unsigned int i = 0; i < intersection.size(); i++)

Also, MOST IMPORTANT ERROR!!!! 另外, 最重要的错误!!!!

for(size_t k = 0; k < v.at(i).size(); k++)
            {
                vec.push_back(k);

            }

Change it to: 将其更改为:

for(size_t k = 0; k < v[i].size(); k++)
            {
                vec.push_back(v[i][k]);

            }

Your while co dition is broken. while共同dition坏了。 You must continue until both indexes are at the end. 您必须继续,直到两个索引都结束。 Currently you stop when one of the indexes has reached the end. 目前,当其中一个索引到达结束时,您将停止。

When you fix that, you also must ensure that you don't increase i beyond n1 ; 当你解决这个问题,你还必须确保你不加i以后n1 ; same for j and n2 . jn2相同。

While you are developing you should rather use vector.at(i) instead of vector[i] to enable safety checks. 在开发过程中,您应该使用vector.at(i)而不是vector[i]来启用安全检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM