64,654
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include<string>
using namespace std;
class IntSet
{
public:
IntSet(){};
IntSet(int *agr,int agr_n);
void Print();
IntSet Merge(IntSet x);
private:
int *a;
int n;
};
IntSet ::IntSet(int *agr,int agr_n)
{
this->a=agr;
this->n=agr_n;
}
void IntSet::Print()
{
int m=0;
for (;m<this->n;m++)
{
cout<<*(this->a+m)<<" ";
}
cout<<endl;
}
IntSet IntSet ::Merge(IntSet x)
{
static int i=this->n, j=0 ,k=0,h,t;
int *temp;
temp=this->a;
for (;j<x.n;j++)
{
for (t=0,h=0;h<i;h++)
{
if (*(x.a+j)==*(temp+h))
{
t=1;
break;
}
}
if (t==0)
{
*(temp+i+k)=*(x.a+j);
k++;
}
}
IntSet temp_IntSet(temp,i+k);
return temp_IntSet;
}
int main()
{
int a[ ]={1,2,3,5,8,10,67,55,9000};
int b[ ]={2,8,9,11,30,56,67,99,88,55};
IntSet set1(a,9),set2(b,10),set3;
cout<<"1:"<<endl;
set1.Print();
set2.Print();
set3.Print();
set3=set1.Merge(set2);
cout<<"2:"<<endl;
set1.Print();
set2.Print();
set3.Print();
return 0;
}
IntSet IntSet ::Merge(IntSet x)
{
static int i=this->n, j=0 ,k=0,h,t;
int *temp;
temp=this->a;
for (;j<x.n;j++)
{
for (t=0,h=0;h<i;h++)
{
if (*(x.a+j)==*(temp+h))
{
t=1;
break;
}
}
if (t==0)
{
*(temp+i+k)=*(x.a+j);//不明所以的代码
k++;
}
}
IntSet temp_IntSet(temp,i+k);//temp是this->a,只有i个int的空间,而你竟然想给他i+k个值
return temp_IntSet;//返回临时对象,已经析构,
}