#include <iostream>
using namespace std;
typedef int Elementtype;
class vector{
Elementtype *data;
int size;
int last;
public:
vector(int sz){
size=sz;
data=new Elementtype [size];
last=0;
}
~vector(){
}
bool full();
bool empty();
bool find(Elementtype elem);
void clear();
void insert(Elementtype num,int j);
void del(int j);
int getlength();
Elementtype getElem(int j);
void Union(vector V2);
void Intersection(vector V2);
void Show();
};
Elementtype vector::getElem(int j){
return data[j];
}
int vector::getlength(){
return last;
}
void vector::clear(){
delete(data);
}
bool vector::full(){
return last==size;
}
bool vector::empty(){
return last==0;
}
void vector::insert(Elementtype num,int j){
for (int i = last-1; i >= j; i--)
{
data[i+1]=data[i];
}
data[j]=num;
last++;
}
void vector::del(int j){
for (int i=j;i<last;i++){
data[i]=data[i+1];
}
last--;
}
bool vector::find(Elementtype elem){
for (int i = 0; i <= last-1; i++)
{
if (elem==data[i])
{
return true;
}
}
return false;
}
void vector::Union(vector V2){
for (int i = 0; i <= V2.last-1; i++)
{
if (!find(V2.data[i]))
{
insert(V2.data[i],last);
}
}
}
void vector::Intersection(vector V2){
for (int i = 0; i <= last-1; )
{
if(!V2.find(data[i])){
del(i);
}else
{
i++;
}
}
}
void vector::Show(){
for (int i = 0; i <= last-1; i++)
{
cout<<data[i]<<" ";
}
cout<<endl;
}
int main(){
vector V1(100);
vector V2(100);
for (int i = 0; i <= 10; i++)
{
V1.insert(i,i);
V2.insert(i+5,i);
}
V1.Show();
V2.Show();
V1.Intersection(V2);
V1.Show();
}
|