POJ C++STL算法部分

C++语言 码拜 8年前 (2016-04-18) 1282次浏览
描述
写一个本人的 CMyistream_iterator 模板,使之能和 istream_iterator 模板达到一样的效果,即:
输入:
79 90 20 hello me
输出:
79
79,90,20
hello,me

#include <iostream>
#include <string>
using namespace std;
// 在此处补充你的代码
template<class T>
class CMyistream_iterator 
{
private:
	T a;
	istream & is;
public:
	CMyistream_iterator(istream & i) :is(i) {
		is >> a;
	}
	T operator * () {
		return a;
	}
	void operator ++ (int) {//这边为什么要加个int呢
		is >> a;
	}
};
int main()
{
	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 << endl;
	CMyistream_iterator<string> inputStr(cin);
	string s1, s2;
	s1 = *inputStr;
	inputStr++;
	s2 = *inputStr;
	cout << s1 << "," << s2 << endl;
	return 0;
}
解决方案

10

引用

//这边为什么要加个int呢

表示是后置++

10

,,,,是的

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明POJ C++STL算法部分
喜欢 (0)
[1034331897@qq.com]
分享 (0)