039:我自己的 ostream_iterator

发布时间:2023年12月17日

描述

程序填空输出指定结果

#include <iostream>
#include <list>
#include <string>
using namespace std;

template <class T1,class T2>
void Copy(T1 s,T1 e, T2 x)
{
	for(; s != e; ++s,++x)
		*x = *s;
}

 
template<class T>
class myostream_iteraotr
{
// 在此处补充你的代码
};


int main()
{	const int SIZE = 5;
	int a[SIZE] = {5,21,14,2,3};
	double b[SIZE] = { 1.4, 5.56,3.2,98.3,3.3};
	list<int> lst(a,a+SIZE);
	myostream_iteraotr<int> output(cout,",");
	Copy( lst.begin(),lst.end(),output); 
	cout << endl;
	myostream_iteraotr<double> output2(cout,"--");
	Copy(b,b+SIZE,output2);
	return 0;
}

输入

输出

5,21,14,2,3,
1.4–5.56–3.2–98.3–3.3–

样例输入

样例输出

同输入

来源

Guo Wei

样例通过

template<class T>
class myostream_iteraotr
{
private:
    ostream & out;
    string str;
public:
    myostream_iteraotr(ostream& _out, const string& _str):out(_out),str(_str) {}
    
    myostream_iteraotr& operator* ()
    {
        return *this;
    }
    
    void operator ++() {}
    
    void operator =(const T & t)
    { out << t << str;}
};
文章来源:https://blog.csdn.net/weixin_45524582/article/details/135036349
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。