两个数组的交集 [英] Intersection of two arrays

查看:123
本文介绍了两个数组的交集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到两个数组的共同元素,将结果存储到第三个数组中并想输出第三个数组。



我尝试过:



 #include< iostream> 
#include< cstdlib>
#include< conio.h>
using namespace std;

int main()
{

int sizeofarray = 0,i = 0,j = 0,num = 0,answers [] = {};
cout<<输入数组的大小<< endl;
cin>> sizeofarray; //从用户获取数组大小
int array1 [sizeofarray];
int array2 [sizeofarray];
cout<<请输入Array1的排序数组成员<< endl;

//数组元素的输入
for(i = 0; i< = sizeofarray; i ++)
{
cin>> array1 [i];
}
system(CLS);
cout<<请输入Array2的排序数组成员<< endl;
for(j = 0; j< = sizeofarray; j ++)
{
cin>> array2 [j];
}
system(CLS);

//比较数组元素并将类似项目存储到另一个数组
while(array1 [i]!= NULL&& array2 [j]!= NULL)
{
if(array1 [i] == array2 [j])
{
answer [num ++] = array1 [i];
i ++;
j ++;
}
else if(array1 [i]< array2 [j])
{
i ++;
} else {
j ++;
}
i ++;
j ++;
}
cout<<公共元素的数量<< num<< endl;
cout<<这些是常见数字:;
for(int k = 0; k< k ++)
{
cout<< answer [k]<<;
}
getch();
返回0;
}

解决方案

注意C ++不支持

<等构造pre lang =c ++> cin>> sizeofarray; // 从用户获取数组大小
int array1 [sizeofarray];



C ++本机数组需要具有编译时已知的大小,因此编译器可以在编译时为其分配内存。如果你想拥有一个可以在运行时重新调整大小的数组,你应该使用std :: vector<>。例如:

 cin>> sizeofarray; 
std :: vector< int> array1(sizeofarray);
< / int>



这同样适用于你的数组array2并回答。



下一个错误是你在进入while循环之前没有重置i和j:

  (array1 [i]!= NULL&& array2 [j]!= NULL)



因此,i和j仍将具有sizeofarray + 1,你将访问超出范围的元素。



条件是那时候是下一个问题。看起来您正在尝试测试数组元素是否包含空指针。但是您的数组包含简单的int值。因此,与NULL的比较没有意义。如果你想测试i或j是否已离开上面的数组边界,你应该将它们与sizeofarray进行比较。



最后,最后一个i ++和j ++看起来不正确对我来说。在调试器中运行您的程序,您将看到我的意思。


i am trying to find common element of two arrays storing the result into 3rd array and want to output third array.

What I have tried:

#include <iostream>
#include <cstdlib>
#include <conio.h>
using namespace std;

int main()
{

    int sizeofarray=0,i=0 ,j=0, num=0, answers[]={};
    cout<<"enter the size of array"<<endl;
    cin>>sizeofarray;//take size of array from user
    int array1[sizeofarray];
    int array2[sizeofarray];
    cout<<"please enter a sorted array member of Array1"<<endl;

    //input of array element
    for ( i=0 ; i<=sizeofarray; i++)
    {
        cin>>array1[i];
    }
    system("CLS");
    cout<<"please enter a sorted array member of Array2"<<endl;
    for (j=0 ; j<=sizeofarray; j++)
    {
        cin>>array2[j];
    }
    system("CLS");

    //comparing the array element and storing the similar items to another array
    while(array1[i]!=NULL && array2[j]!=NULL)
    {
        if(array1[i]==array2[j])
        {
            answer[num++]=array1[i];
            i++;
            j++;
        }
        else if(array1[i]<array2[j])
        {
            i++;
        }else{
            j++;
        }
        i++;
        j++;
    }
    cout<<"The number of common elements"<<num<<endl;
    cout<<"These are the common numbers: ";
    for (int k=0;k<num;k++)
    {
        cout<<answer[k]<<" ";
    }
    getch();
    return 0;
}

解决方案

Note the C++ does not support constructs like

cin>>sizeofarray;//take size of array from user
int array1[sizeofarray];


C++ native arrays need to have a size that is known at compile time, so the compiler can allocate memory for it at compile time. If you want to have an array that you can re-size at runtime you should use std::vector<>. For example:

    cin >> sizeofarray;
    std::vector<int> array1 (sizeofarray);
</int>


The same applies to your arrays array2 and answer.

The next error is that you don't reset i and j before entering the while loop:

while(array1[i]!=NULL && array2[j]!=NULL)


Hence, i and j will still have the value of sizeofarray+1 and you will be accessing elements that are out of bounds.

The condition in that while is the next problem. It looks like you are trying to test whether array elements contain a null pointer. But your array contain simple int values. Hence the comparison with NULL does not make sense. If you want to test whether i or j have left the upper array boundary you should compare them with sizeofarray.

Finally, the last i++ and j++ don't appear right to me. Run your program in a debugger and you will see what I mean.


这篇关于两个数组的交集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆