Problem E: 向量的删除

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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值