- 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
- 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
- 4、该文档为VIP文档,如果想要下载,成为VIP会员后,下载免费。
- 5、成为VIP后,下载本文档将扣除1次下载权益。下载后,不支持退款、换文档。如有疑问请联系我们。
- 6、成为VIP后,您将拥有八大权益,权益包括:VIP文档下载权益、阅读免打扰、文档格式转换、高级专利检索、专属身份标志、高级客服、多端互通、版权登记。
- 7、VIP文档为合作方或网友上传,每下载1次, 网站将根据用户上传文档的质量评分、类型等,对文档贡献者给予高额补贴、流量扶持。如果你也想贡献VIP文档。上传文档
查看更多
CPrimer第16章-模板与泛型编程-部分选做习题答案
16章 部分选做习题
16.1编写一个模板返回形参的绝对值,至少用三种不同类型的值调用模板。注意:在16.3节讨论编译器怎样处理模板实例化之前,你应该将每个模板定义和该模板的所有使用放在同一文件中。
// 16.1_template.cpp : 定义控制台应用程序的入口点。
//
#include stdafx.h
#include iostream
template typename T inline T abs( T tVal)
{
return tVal 0 ? tVal : -1*tVal;
}
int _tmain(int argc, _TCHAR* argv[])
{
std::cout \n\tabs( false ) is:\t abs(false) std::endl
\n\tabs( -3 ) is:\t\t abs( -3 ) std::endl
\n\tabs( -8.6 ) is:\t\t abs( -8.6 ) std::endl;
system(pause);
return 0;
}
16.2 编写一个函数模板,接受一个ostream引用和一个值,将该值写入流。用至少四种不同类型调用函数。通过写至cout、写至文件和写至stringstream来测试你的程序。
// 16.2_template.cpp : 定义控制台应用程序的入口点。
//
#include stdafx.h
#include iostream
#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 Test of testOstream( outfile, 6 ):\t);
testOstream( outfile, 6 );
testOstream(outfile, \n Test of testOstream( outfile, true ):\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);
testOstream( outfile, Good! );
outfile.close();
cout endl;
// stringstream
stringstream oss;
cout \n Test of testOstream( oss, 6 )/( oss, true )/( oss, 8.6 )/( oss, \Good!\): endl;
testOstream( oss, 6 );
testOstream( oss, \n );
testOstream( oss, t
您可能关注的文档
最近下载
- 高压除氧器培训课件.ppt VIP
- 最新行政执法题库行政执法试题及答案附答案详解(夺分金卷).docx VIP
- 最新行政执法题库行政执法试题及答案附答案详解(综合卷).docx VIP
- 最新行政执法题库行政执法试题及答案附答案详解(a卷).docx VIP
- 时速160客货共线铁路简支T梁-24m 通桥(2012)2101-II.pdf VIP
- 解密华为人资资源管理题库.docx VIP
- 最新行政执法题库行政执法试题及答案附答案详解(典型题).docx VIP
- 最新行政执法题库行政执法试题及答案附答案详解(基础题).docx VIP
- 大学生职业生涯规划.pdf VIP
- 个人电脑抵押合同4篇.docx VIP
原创力文档


文档评论(0)