- 1、本文档共10页,可阅读全部内容。
- 2、有哪些信誉好的足球投注网站(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
- 5、该文档为VIP文档,如果想要下载,成为VIP会员后,下载免费。
- 6、成为VIP后,下载本文档将扣除1次下载权益。下载后,不支持退款、换文档。如有疑问请联系我们。
- 7、成为VIP后,您将拥有八大权益,权益包括:VIP文档下载权益、阅读免打扰、文档格式转换、高级专利检索、专属身份标志、高级客服、多端互通、版权登记。
- 8、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
您可能关注的文档
- 贯彻双代会精神工作规划措施.doc
- 18b20温度测量.doc
- 2009年陕西省初中毕业学业考试数学试题.doc
- 现代医院简报第11期(献血).doc
- 线性代数2008B答案.doc
- 心理咨询师国家考试.doc
- windowsserver2008服务器备份文件共享(权限问题).doc
- 小区住宅防水工程技术交底.doc
- 绿化单项养护工作标准及检查办法.doc
- 什么是商品期货.doc
- 2025北京门头沟区教育系统事业单位第三次招聘教师26人笔试备考题库及答案解析.docx
- 2025安徽宿州砀山县图书馆招聘30人笔试备考试题及答案解析.docx
- 2025春季广西工贸职业技术学校招聘编外人员17人笔试模拟试题及答案解析.docx
- 2025北京华润建材科技总部战略管理部副总经理招聘1人笔试备考试题及答案解析.docx
- 2025福建南平松溪县城市资产投资运营有限公司招聘项目内业人员2人笔试备考题库及答案解析.docx
- 2025福建泉州市晋江产业权属子公司招聘项目制补充笔试模拟试题及答案解析.docx
- 2025安徽黄山祁门县选聘社区工作者14人笔试备考试题及答案解析.docx
- 2025《人民铁道》报业有限公司招聘高校毕业生1人(二)笔试备考题库及答案解析.docx
- 2025福建莆田农商银行暑期大学生实习招募200人笔试备考题库及答案解析.docx
- 2025福建莆田农商银行暑期大学生实习招募200人笔试模拟试题及答案解析.docx
最近下载
- 智能网联汽车智能座舱系统装调与测试 项目三 抬头显示系统.pptx VIP
- 人教版六年级下册数学教学工作总结.docx VIP
- 智能网联汽车智能座舱系统装调与测试 项目一 智能座舱认识.pptx VIP
- 220V/100A三相半波可控整流电路设计.docx
- 智能网联汽车智能座舱系统装调与测试 项目二 OTA与远程控制系统.pptx VIP
- 危害分析与关键控制点(HACCP)课件.ppt VIP
- 西南石油大学939管理学原理历年考研真题16-21年.pdf
- 农村安全饮水工程监理大纲192页.doc VIP
- 水稻栽培的生物学基础.pptx VIP
- 山东省滨州市邹平市2022-2023学年八年级下学期期末数学试题(含答案).docx VIP
文档评论(0)