STL编程题2(C++程序设计第9周)
2016-01-16 00:06
459 查看
描述
写一个自己的 CMyistream_iterator 模板,使之能和 istream_iterator 模板达到一样的效果,即:
输入:
79 90 20 hello me
输出:
79
79,90,20
hello,me
输入
79 90 20 hello me
输出
79
79,90,20
hello,me
样例输入
样例输出
提示
istream_iterator模版使用说明:
其构造函数执行过程中就会要求输入,然后每次执行++,则读取输入流中的下一个项目,执行 * 则返回上次从输入流中读取的项目。例如,下面程序运行时,就会等待用户输入数据,输入数据后程序才会结束:
下面程序运行时,如果输入 12 34 程序输出结果是: 12,12
下面程序运行时,如果输入 12 34 56程序输出结果是: 12,56
源码
写一个自己的 CMyistream_iterator 模板,使之能和 istream_iterator 模板达到一样的效果,即:
输入:
79 90 20 hello me
输出:
79
79,90,20
hello,me
#include <iostream> #include <string> using namespace std; // 在此处补充你的代码 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; }
输入
79 90 20 hello me
输出
79
79,90,20
hello,me
样例输入
79 90 20 hello me
样例输出
79 79,90,20 hello,me
提示
istream_iterator模版使用说明:
其构造函数执行过程中就会要求输入,然后每次执行++,则读取输入流中的下一个项目,执行 * 则返回上次从输入流中读取的项目。例如,下面程序运行时,就会等待用户输入数据,输入数据后程序才会结束:
#include <iostream> #include <iterator> using namespace std; int main() { istream_iterator<int> inputInt(cin); return 0; }
下面程序运行时,如果输入 12 34 程序输出结果是: 12,12
#include <iostream> #include <iterator> using namespace std; int main() { istream_iterator<int> inputInt(cin); cout << * inputInt << "," << * inputInt << endl; return 0; }
下面程序运行时,如果输入 12 34 56程序输出结果是: 12,56
#include <iostream> #include <iterator> using namespace std; int main() { istream_iterator<int> inputInt(cin); cout << * inputInt << "," ; inputInt ++; inputInt ++; cout << * inputInt; return 0; }
源码
#include <iostream> #include <string> using namespace std; // 在此处补充你的代码 template <class T> class CMyistream_iterator { public: CMyistream_iterator(istream& input):is(input) { is >> a; } T operator*() { return a; } void operator++(int) { is >> a; } private: T a; istream& is; }; 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; }
相关文章推荐
- 初步剖析C语言编程中的结构体
- C、C++中关键字static的区别
- C/C++程序编译的内存分配情况
- 用C++设计一个不能被继承的类
- STL编程题1(C++程序设计第1周)
- Max Sum (HD_1003) 基础DP
- 【C语言】三种方法实现strlen函数
- 类的初步知识(C++中直角坐标和极坐标之间的转换代码)
- 用c语言指针实现给整形数组冒泡排序
- 一起talk C栗子吧(第一百零二回:C语言实例--使用信号量进行进程间同步与互斥三)
- 【C语言】字符串替换空格:请实现一个函数,把字符串中的每个空格替换成“%20”。
- iOS 学习第八天 C语言 二维数组
- 【LeetCode-326】Power of three 上(C++)
- switch使用总结
- 初探C++流
- C/C++小知识
- C++实现二叉树(二)
- libsvm如何调用svm.cpp进行训练和预测的具体过程
- 一个计算器的C语言实现
- PVS-Studio C/C++/C++11 静态代码分析工具