C++ 模板类 005:山寨版istream_iterator

005:山寨版istream_iterator

描述
模仿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组数据
每组数据一行,三个整数加两个字符串。字符串是不含空格的
输出
对每组数据,输出二行
在第一行输出第一个数
第二行原样输出输入的内容
提示
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

分析main函数中输出
该题主要复习了运算符重载,每个成员函数的返回值都要注意

int main()  
{ 
	int t;
	cin >> t;       //T组数据,每组3个整数+2个字符串 
	while( t -- ) {
		 CMyistream_iterator<int> inputInt(cin);     //Ci对象 inputInt 
		 int n1,n2,n3;
		 n1 = * inputInt; //读入 n1         
		 int tmp = * inputInt;        
		 cout << tmp << endl;        //79 
		 inputInt ++;              //inpuntInt 数组+1 
		 n2 = * inputInt; //读入 n2        //90 
		 inputInt ++;                     
		 n3 = * inputInt; //读入 n3              //20 
		 cout << n1 << " " << n2<< " " << n3 << " ";          //79 90 20 
		 CMyistream_iterator<string> inputStr(cin);   //string 模板 
		 string s1,s2;
		 s1 = * inputStr;            //hello 
		 inputStr ++;
		 s2 = * inputStr;      //me
		 cout << s1 << " " << s2 << endl;     //hello me
	}
	 return 0;  
}

私有成员

	istream &  r;           
	T v;

1.main函数中构建CMyistream_iterator对象 inputInt(cin),故cin为构造函数的参数,类型istream &

public:
		CMyistream_iterator( istream & rr):r(rr) {		
				r >> v ;                     
		}

2.赋值n1时用*inputInt,故要重载 * 返回n1的类型,即模板类型T;

		T operator *() {    //*()重载 ,用于前置形式 
							//* inputInt相当于 inputInt.operator *() 
			return v;	     //返回T类型的v
		}

友元重载 * ,功能与上面相同

friend T operator *(CMyistream_iterator<T> f) {    //*重载 ,*一个对象为得到对象的v 
			return f.v;	     //返回 T 类型的v
		}
//相当于全局设定,全局设定比成员函数多一个形参 

3.inputInt++,故需重载后置++,实现读入操作,无赋值操作故无需返回类型

void operator ++ (int) {      //重载++ 
									//后置形式——括号中int无用 
			 r >> v ;               //inputInt ++ 等价于 inputInt.operator ++(0) 
		}

完整代码

#include <iostream>
#include <string>

using namespace std;
template <class T>
class CMyistream_iterator
{
// 在此处补充你的代码
//设置inputInt 
	istream &  r;            //cin类 成员 
	T v;                     
	public:
		//	 n1 = * inputInt; //读入 n1         // 79 返回n1的类型——即T 
		T operator *() {    //*()重载 ,用于前置形式 
							//* inputInt相当于 inputInt.operator *() 
			return v;	     //得到v 
		}
		CMyistream_iterator( istream & rr):r(rr) {   //构造函数 //赋予r意义 
			r >> v ;                        //r到v中 
		}
		void operator ++ (int) {      //重载++ 
									//后置形式——括号中int无用 
			 r >> v ;               //inputInt ++ 等价于 inputInt.operator ++(0) 
		}
//
};
int main()  
{ 
	int t;
	cin >> t;       //T组数据,每组3个整数+2个字符串 
	while( t -- ) {
		 CMyistream_iterator<int> inputInt(cin);     //Ci对象 inputInt 
		 int n1,n2,n3;
		 n1 = * inputInt; //读入 n1         // 79
		 int tmp = * inputInt;
		 cout << tmp << endl;        //79 
		 inputInt ++;              //inpuntInt 数组+1 
		 n2 = * inputInt; //读入 n2        //90 
		 inputInt ++;                     
		 n3 = * inputInt; //读入 n3              //20 
		 cout << n1 << " " << n2<< " " << n3 << " ";          //79 90 20 
		 CMyistream_iterator<string> inputStr(cin);   //string 模板 
		 string s1,s2;
		 s1 = * inputStr;            //hello 
		 inputStr ++;
		 s2 = * inputStr;      //me
		 cout << s1 << " " << s2 << endl;     //hello me
	}
	 return 0;  
}

注意事项
自定义类的对象的符号操作,都是要重载的。
这里的* 并不是平常的 T*类型,
这里的++也并不是int ++。

发布了77 篇原创文章 · 获赞 3 · 访问量 3053

猜你喜欢

转载自blog.csdn.net/BLUEsang/article/details/105176492
今日推荐