C:山寨版istream_iterator
- 總時間限制:
- 1000ms
- 內存限制:
- 65536kB
- 描述
-
模仿C++標准模板庫istream_iterator用法,實現CMyistream_iterator使得程序按要求輸出
-
#include <iostream> #include <string> using namespace std; template <class T> class CMyistream_iterator {
// 在此處補充你的代碼
-
}; int main() { int t; cin >> t; while( t -- ) { CMyistream_iterator<int> inputInt(cin); int n1,n2,n3; n1 = * inputInt; //讀入 n1 int tmp = * inputInt; cout << tmp << endl; inputInt ++; n2 = * inputInt; //讀入 n2 inputInt ++; n3 = * inputInt; //讀入 n3 cout << n1 << " " << n2<< " " << n3 << " "; CMyistream_iterator<string> inputStr(cin); string s1,s2; s1 = * inputStr; inputStr ++; s2 = * inputStr; cout << s1 << " " << s2 << endl; } return 0; }
- 輸入
-
第一行是整數t,表示有t組數據
每組數據一行,三個整數加兩個字符串。字符串是不含空格的 - 輸出
-
對每組數據,輸出二行
在第一行輸出第一個數
第二行原樣輸出輸入的內容 - 樣例輸入
-
2 79 90 20 hello me 12 34 19 take up
- 樣例輸出
-
79 79 90 20 hello me 12 12 34 19 take up
- 提示
-
C++標准模板庫 istream_iterator模版使用說明:
其構造函數執行過程中就會要求輸入,然后每次執行++,則讀取輸入流中的下一個項目,執行 * 則返回上次從輸入流中讀取的項目。例如,下面程序運行時,就會等待用戶輸入數據,輸入數據后程序才會結束:
#include
#include
using namespace std;
int main() {
istream_iterator inputInt(cin);
return 0;
}
下面程序運行時,如果輸入 12 34 程序輸出結果是: 12,12
#include
#include
using namespace std;
int main()
{
istream_iterator inputInt(cin);
cout << * inputInt << "," << * inputInt << endl;
return 0;
}
下面程序運行時,如果輸入 12 34 56程序輸出結果是: 12,56
#include
#include
using namespace std;
int main()
{
istream_iterator inputInt(cin);
cout << * inputInt << "," ;
inputInt ++;
inputInt ++;
cout << * inputInt;
return 0;
} - 來源
- Guo Wei
- 郭老師出的蜜汁題目,反正想山寨誰就先在成員變量里寫上誰。。。
-
public:
istream_iterator<T> a;
CMyistream_iterator<T>(istream&is){
istream_iterator<T> in(is);
a=in;
}
ostream& operator<<(ostream&os){
os<<*a;
return os;
}
friend T operator*(CMyistream_iterator<T> b){
return *b.a;
}
friend CMyistream_iterator operator++(CMyistream_iterator<T> &b,int k){
b.a++;
return b;
}
然鵝,並過不了,仔細一看openjudge說compile error,本地卻運行無誤。想必是因為沒有#include<iterator>
看了一下群里大佬,琢磨出一個騷皮寫法:public:
istream *a;
T *p;
CMyistream_iterator<T>(istream&is){
p=new T[5];
a=&is;
*a>>*p;
}
ostream& operator<<(ostream&os){
os<<*p;
return os;
}
friend T operator*(CMyistream_iterator<T> b){
return *b.p;
}
friend CMyistream_iterator operator++(CMyistream_iterator<T> &b,int k){
b.p++;
*b.a>>*b.p;
return b;
}
原理是每次++時就讀入。。。那兩個函數不寫成friend也成