標籤:friend iter remove geo sub input stream operator opera
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
101 2 3 4 5 1 2 3 4 51
Sample Output
1 2 3 4 5 1 2 3 4 52 3 4 5 2 3 4 5
HINT Append Codeappend.cc,
#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;}
Problem X: 刪除數組元素