描述
写一个本人的 CMyistream_iterator 模板,使之能和 istream_iterator 模板达到一样的效果,即:
输入:
79 90 20 hello me
输出:
79
79,90,20
hello,me
写一个本人的 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
表示是后置++
10
,,,,是的