Problem X: 删除数组元素

Problem X: 删除数组元素

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 375  Solved: 151
[Submit][Status][Web Board]

Description

定义Array类,其中只有一个int类型的数组,数组元素个数未知。

重载其<<、>>、-运算符。其中"<<"输出所有的数组元素,两两之间用1个空格隔开;">>"根据输入的格式读取数组元素;"-"接收一个int类型的参数a,将数组中与a相等的元素删除。

Input

输入有3行。第一行N>0;第二行是N个整数,是数组元素;第3行是一个int类型数,是需要从数组中删除的数。

Output

见样例。

Sample Input

10
1 2 3 4 5 1 2 3 4 5
1

  

Sample Output

1 2 3 4 5 1 2 3 4 5
2 3 4 5 2 3 4 5

  

HINT

 

Append Code

#include <iostream>
#include <list>
using namespace std;
class Array
{
public :
    list<int> num;
    friend istream &operator>>(istream &is, Array &p)
    {
        int n; is>>n;
        for(int i=0; i<n; i++)
        {
            int temp; is>>temp;
            p.num.push_back(temp);
        }
        return is;
    }
    friend ostream &operator<<(ostream &os, Array &p)
    {
        list<int>::iterator pp;
        for(pp=p.num.begin(); pp!=p.num.end(); pp++)
        {
            if(pp==p.num.begin())
                os<<*pp;
            else
                os<<" "<<*pp;
        }
        os<<endl;
        return os;
    }
    Array &operator-(int a)
    {
        num.remove(a);
        return *this;
    }
};
int main()
{
    int a;
    Array arr;
    cin>>arr;
    cout<<arr;
    cin>>a;
    arr = arr - a;
    cout<<arr;
    return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/Jie-Fei/p/9153748.html
今日推荐