C++ 模板经典练习

总时间限制: 

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

 

这道题目具有一定难度,从构造函数传入的参数可以知道,这里是一个类的委托设计,私有成员变量存放一个istream的引用。私有成员变量维护一个模板元素 T的指针,用来保存读到的数据,这里*是一个运算符重载,重载读取操作。++也是一个重载,用来删除这个数。

 

#include <iostream>
#include <string>

using namespace std;
template <class T>
class CMyistream_iterator
{
	// 在此处补充你的代码
private:
	T * ptr;
	istream & is;
public:
	CMyistream_iterator(istream &is_) :is(is_)
	{
		ptr = nullptr;
	}
	T & operator *()
	{
		if (ptr)
			return *ptr;
		ptr = new T;
		is >> *ptr;
		return *ptr;
	}
	void operator++(int)//需要重载的后置的++运算符
	{
		delete ptr;
		ptr = nullptr;
	}
	~CMyistream_iterator()
	{
		if (ptr)
			delete ptr;
	}
	// 在此结束补充的代码
};

int main()
{
	int t;
	cin >> t;
	while (t--) {
		//T = int,实参cin,参数类型为&
		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;
}

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值