Problem E: 向量的删除
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 275 Solved: 168
Description
定义Vec类,是由int类型的数据组成的向量,重载其输入、输出运算符,以及减法运算符。
其中,输入一个整型向量时,输入的是一个非减排序的整数序列,其中包含重复值,在输入时,重复值只保留1个,即Vec类中的向量是递增排序且不含重复值的。
输出时,两两之间用一个空格隔开。
减法运算,从第1个Vec对象中删除第2个Vec对象中的元素,不能修改两个操作数的值。
注意:删除后,有可能结果为空集。
Input
输入有2行。每行是一个向量。
每行第一个值是一个正整数N>0,表示后面有N个输入的整数。
Output
见样例。
Sample Input
10 1 1 1 3 3 3 4 5 8 85 1 2 3 4 5
Sample Output
v1:1 3 4 5 8v2:1 2 3 4 5v1:1 3 4 5 8v2:1 2 3 4 5v3:8
HINT
Append Code
#include <bits/stdc++.h>
using namespace std;
class Vec
{
public:
int t;
set<int> p;
Vec(){}
friend istream &operator>>(istream &is,Vec &A)
{
int m,n;
cin>>m;
A.t=m;
while(m--)
{
cin>>n;
A.p.insert(n);
}
return is;
}
friend ostream &operator<<(ostream &os,Vec &A)
{
set<int>::iterator it;
for(it=A.p.begin();it!=A.p.end();it++)
{
if(it==A.p.begin())
cout<<*it;
else
cout<<" "<<*it;
}
cout<<endl;
return os;
}
friend Vec operator-(Vec &A,Vec &B)
{
Vec C=A;
set<int>::iterator it;
set<int>::iterator iter;
for(it=B.p.begin();it!=B.p.end();it++)
{
iter=C.p.find(*it);
if(iter!=C.p.end())
C.p.erase(iter);
}
return C;
}
};
int main()
{
Vec v1, v2, v3;
cin>>v1;
cin>>v2;
cout<<"v1:"<<v1;
cout<<"v2:"<<v2;
v3 = v1 - v2;
cout<<"v1:"<<v1;
cout<<"v2:"<<v2;
cout<<"v3:"<<v3;
return 0;
}