欢迎来到天天文库
浏览记录
ID:40224414
大小:160.97 KB
页数:12页
时间:2019-07-27
《第3章汇编语言程序格式》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、1Chapter1C++TemplatesReadings:Sections1.6and1.72TemplatesType-independentpatternsthatcanworkwithmultipledatatypes.GenericprogrammingCodereusableFunctionTemplatesThesedefinelogicbehindthealgorithmsthatworkformultipledatatypes.ClassTemplatesThesedefinegenericclas
2、spatternsintowhichspecificdatatypescanbepluggedintoproducenewclasses.3FunctionTemplatesExampleGenericfunctiontofindamaximumvalueinagivenvectorIfargumentisavectortype,thencompilergeneratesacorrespondingfunctionwhereComparableisreplacedbyinttype.Similarlyfor
3、vector,vectoretc.Assumptioninthisexample:Operator4FunctionTemplatesUsageEachcalltofindMax()onadifferentdatatypeforcesthecompilertogenerateadi
4、fferentfunctionusingthetemplate.CompilerwillcomplainaboutfindMax(v4)becauseIntCellclassdoesnotdefinedoperator5、t{return4*side;}voidprint(ostream&out=cout)const{out<<"(square"<6、ut);returnout;}5AnexampleOperatorOverloadingComparisonoperator7、arator>constObject&findMax(constvector&arr,Comparatorcmp){intmaxIndex=0;for(inti=1;i8、)const{returnstrcasecmp(lhs.c_str(),rhs.c_str())<0;}};intmain(){vectorarr={"ZEBRA","alligator","crocodile"};cout<
5、t{return4*side;}voidprint(ostream&out=cout)const{out<<"(square"<6、ut);returnout;}5AnexampleOperatorOverloadingComparisonoperator7、arator>constObject&findMax(constvector&arr,Comparatorcmp){intmaxIndex=0;for(inti=1;i8、)const{returnstrcasecmp(lhs.c_str(),rhs.c_str())<0;}};intmain(){vectorarr={"ZEBRA","alligator","crocodile"};cout<
6、ut);returnout;}5AnexampleOperatorOverloadingComparisonoperator7、arator>constObject&findMax(constvector&arr,Comparatorcmp){intmaxIndex=0;for(inti=1;i8、)const{returnstrcasecmp(lhs.c_str(),rhs.c_str())<0;}};intmain(){vectorarr={"ZEBRA","alligator","crocodile"};cout<
7、arator>constObject&findMax(constvector&arr,Comparatorcmp){intmaxIndex=0;for(inti=1;i8、)const{returnstrcasecmp(lhs.c_str(),rhs.c_str())<0;}};intmain(){vectorarr={"ZEBRA","alligator","crocodile"};cout<
8、)const{returnstrcasecmp(lhs.c_str(),rhs.c_str())<0;}};intmain(){vectorarr={"ZEBRA","alligator","crocodile"};cout<
此文档下载收益归作者所有