blunder

导航

山寨istream_iterator

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也成

posted on 2018-04-01 22:30  blunder  阅读(1554)  评论(0编辑  收藏  举报