C++模板与STL库.pptVIP

  1. 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
  2. 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  3. 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
  4. 4、该文档为VIP文档,如果想要下载,成为VIP会员后,下载免费。
  5. 5、成为VIP后,下载本文档将扣除1次下载权益。下载后,不支持退款、换文档。如有疑问请联系我们
  6. 6、成为VIP后,您将拥有八大权益,权益包括:VIP文档下载权益、阅读免打扰、文档格式转换、高级专利检索、专属身份标志、高级客服、多端互通、版权登记。
  7. 7、VIP文档为合作方或网友上传,每下载1次, 网站将根据用户上传文档的质量评分、类型等,对文档贡献者给予高额补贴、流量扶持。如果你也想贡献VIP文档。上传文档
查看更多
注意: ●模板并非通常意义上可直接使用的函数或类,它仅仅是对一族函数或类的描述,是参数化的函数和类。 ●模板是一种使用无类型参数来产生一族函数或类的机制。 2、模板的实例化 ●模板通过参数实例化可以构建具体的函数或类,称为模板函数和模板类。 #include iostream.h template class Type1, class Type2 void myfunc(Type1 x, Type2 y) { cout x y \n; } int main() { myfunc(10, hi); myfunc(0.23, 10L); return 0; } #include string templateclass T T max(T a,T b) { return (ab)?a:b; } templateclass T T max(T a,T b,T c) { T t; t=(ab)?a:b; return (tc)?t:c; } char* max(char* a,char* b) { return (strcmp(a,b)0)?a:b; } #include iostream.h main() { int x=10,y=20,max1; double a=10.4,b=21.3,c=13.4,max2; max1=max(x,y); max2=max(a,b,c); coutThe maxinum of x and y is: max1endl; coutThe maxinum of a and b and c is: max2endl; char *c1,*c2,*c3; c1=aaaa; c2=bbbb; c3=max(c1,c2); coutThe maxinum of c1 and c2 is: c3endl; return 0; } 举 例 例8-4 类模板的应用 例8-5 含有成员函数的类模板定义 #include iostream #include vector #include algorithm #include numeric using namespace std; void main() { istream_iteratorint input(cin); int n1,n2; n1 = *input; input++; n2 = *input; coutn1,n2endl; ostream_iteratorint output(cout,**); *output=n1 + n2; coutendl; } #include list #include iostream using namespace std; int main() { listint v; v.push_front(1); v.push_front(5); v.push_front(3); v.push_front(4); v.push_front(3); v.push_front(4); //v.pop_front(); //v.remove(3); v.sort(); v.unique(); 举 例 例8-7 利用STL提供的容器和算法,对数组进行倒序输出 例8-8 利用STL求任意指定整数以内的全部素数 例8-9 比较数据大小并排序 #include queue #include iostream using namespace std; int main() { priority_queuedouble priorities; priorities.push(3.2); priorities.push(9.8); priorities.push(5.4); while( !priorities.empty() ) { cout priorities.top() ; priorities.pop(); } return 0; } //输出结果:9.8 5.4 3.2 * 排序和查找算法 Sort templateclass RanIt void sort(RanIt first, RanIt last); find templateclass InIt, class T InIt find(InIt first, InIt last, const T val); binary_search 折半查找,要求容器已经有序 templateclass FwdIt, class

文档评论(0)

chaofeng7458 + 关注
实名认证
文档贡献者

专利代理人资格证持证人

该用户很懒,什么也没介绍

领域认证该用户于2023年03月15日上传了专利代理人资格证

1亿VIP精品文档

相关文档