//编写一个函数模板,接受一个ostream引用和一个值,将该值写入流,用至少四种不同类型调用函数。
//通过写至cout,写至文件和写至stringstream来测试
#include<iostream>
#include<tchar.h>
#include<ostream>
#include<fstream>
#include<sstream>
#include<cassert>
using namespace std;
template <typename t> inline
void testostream(ostream& os, t tval)
{
os << tval;
}
int _tmain(int argc, _TCHAR* argv[])
{
//cout
cout << "test of testostream(cout,6):\t";
testostream(cout, 6);
cout << "\n test of testostream(cout,true):\t";
testostream(cout, true);
cout << "\n test of testostream(cout,8.6):\t";
testostream(cout, 8.6);
cout << "\n test of testostream(cout,\"good\"):\t";
testostream(cout, "good");
cout << endl;
//ofstream
ofstream outfile;
outfile.open("testof_ofstream.txt");
assert(outfile);
if (!outfile)
{
return NULL;
}
cout << "\n\ttest of ofstream,please check the file just create.\n";
testostream(outfile, "\n tset of testostream(outfile,6):\t");
testostream(outfile, 6);
testostream (outfile, "\n test of testostream(outfile,ture):\t");
testostream(outfile, true);
testostream(outfile, "\n test of testostream(outfile,8.6):\t");
testostream(outfile, 8.6);
testostream(outfile,"\n test of testostream(outfile,\"good\"):\t");
outfile.close();
cout << endl;
//stringstream
stringstream oss;
cout << "\n test of testosteam(oss,6)/(oss,8.6)/(oss,\"good\"):" << endl;
testostream(oss, 6);
testostream(oss, "\n");
testostream(oss, true);
testostream(oss, "\n");
testostream(oss, 8.6);
testostream(oss, "\n");
testostream(oss, "good");
testostream(oss, "\n");
cout << oss.str() << endl;
system("pause");
return 0;
}
primer4 16.2
最新推荐文章于 2023-03-06 21:05:08 发布