编写函数模板,接受一个ostream引用和一个值,将该值写入流。用至少四种不同类型调用函数,通过写至cout、写至文件和写至string stream来测试程序。
#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
using namespace std;
template<typename T1,typename T2>//接受两个参数
T1& print(T1 & s, T2 val)
{
s << val;
return s;
}
int main()
{
double dval = 3.23;
float fval = 1.2;
string oristr = "this is a test",desstr;
ostringstream oss(desstr);
ofstream outFile("result.dat");
//写至cout
print(cout ,-3) << endl;
print(cout ,dval) << endl;
print(cout ,fval) << endl;
print(cout ,oristr) << endl;
//写至文件
print(outFile ,-3) << endl;
print(outFile ,dval) << endl;
print(outFile ,fval) << endl;
print(outFile ,oristr) << endl;
outFile.close();
//写至stringstream
print(oss ,-3) << endl;
print(oss ,dval) << endl;
print(oss ,fval) << endl;
print(oss ,oristr) << endl;
//验证
cout << oss.str() << endl;
system("pause");
return 0;
}