primer4 16.2

来源:互联网 发布:淘宝上差评多久消失 编辑:程序博客网 时间:2024/06/05 03:04
//编写一个函数模板,接受一个ostream引用和一个值,将该值写入流,用至少四种不同类型调用函数。//通过写至cout,写至文件和写至stringstream来测试#include<iostream>#include<tchar.h>#include<ostream>#include<fstream>#include<sstream>#include<cassert>using namespace std;template <typename t> inlinevoid testostream(ostream& os, t tval){os << tval;}int _tmain(int argc, _TCHAR* argv[]){//coutcout << "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;//ofstreamofstream 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;//stringstreamstringstream 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;}

原创粉丝点击