 
         
         
         
         
        
            免费预览已结束,剩余44页可下载查看            
        
         下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
            模板与标准模板库 代码重用的另一种方式 模板类7 1模板的基本知识一个模板类至少具有一个类参数 类参数是个符号以表示将要被某个确定数据类型代替的类型 classintArray public int private int a intsize intArray foremphasisintdummy val arbitraryvalue int constint 模板类方式 templateclassArray public T templateT templateconstT templateArray Array ints a newT size s templateArray Array delete a templateostream 7 1 1模板实例 includeusingnamespacestd declarationoftemplateclassArrayintmain Arraya1 100 arrayof100doublesa1 6 3 14 cout a1 6 endl 例子 templateclassArray Arraya0 50 Arraya1 50 templateArraya2 50 Arraya3 50 7 1 2参数表中的模板类模板类可以作为一种数据类型出现在参数表中 templateostream 7 2函数模板templateTMax Tt intn inti 0 Ttemp t 0 for i 1 i n i if temp t i temp t i returntemp templatevoidswap T 7 3标准模板库STL标准模板库 STL 是标准C 库的一部分 7 3 1容器 算法和迭代器STL容器 vectorstackqueuedequelistsetmap等STL算法 copysortsearchmergepermute等STL提供了多种类型的迭代器 可分别进行正向 反向 双向和随机遍历操作 7 3 2STL的优越性 intsq int voidmain vectorv 10 fillupvwithvalue for inti 0 i 10 i v push back i for each v begin v end sq intsq inta returna a include include includeusingnamespacestd voidmain vectorv 10 dequed sort v beging v end sort d begin d end 7 3 3容器基础知识STL基本容器可以分为两组 序列式容器和关联式容器 序列式容器如同数组 可以通过下标来访问 listvectordeque 关联式容器中的元素可以通过键值 key 来访问 关联式容器可以将任意类型的数据作为键值来使用 mapmultimapsetmultiset floatgnp m China include includeintmain usingnamespacestd vector iteratorv1 Iter v2 Iter v3 Iter v4 Iter v5 Iter Createanemptyvectorv0vectorv0 Createavectorv1with3elementsofdefaultvalue0vectorv1 3 Createavectorv2with5elementsofvalue2vectorv2 5 2 Createacopy vectorv4 ofvectorv2vectorv4 v2 vectorv5 v4 begin 1 v4 begin 3 cout v1 for v1 Iter v1 begin v1 Iter v1 end v1 Iter cout v1 Iter cout endl cout v2 for v2 Iter v2 begin v2 Iter v2 end v2 Iter cout v2 Iter cout endl cout v4 for v4 Iter v4 begin v4 Iter v4 end v4 Iter cout v4 Iter cout endl cout v5 for v5 Iter v5 begin v5 Iter v5 end v5 Iter cout v5 Iter cout endl 7 3 4基本序列式容器 vector deque和list include includeusingnamespacestd intmain inti vectornums nums insert nums begin 999 nums insert nums begin 15 nums insert nums end 60 for i 0 i nums size i cout nums i endl 15 99960cout endl nums erase nums begin 99960nums erase nums begin 60for i 0 i nums size i cout nums i endl 60return0 include include includeusingnamespacestd voiddump list names reverse cout include includeusingnamespacestd intmain vectorc1 vector iteratorc1 Iter vector const iteratorc1 cIter c1 push back 1 c1 push back 2 c1 Iter c1 begin cout Thefirstelementofc1is c1 Iter endl c1 Iter 20 c1 Iter c1 begin cout Thefirstelementofc1isnow c1 Iter endl Thefollowinglinewouldbeanerrorbecause iteratorisconstc1 cIter c1 begin c1 cIter 200 7 3 5vector deque和list的效率比较7 3 6基本关联式容器 set multiset map和multimap include includeusingnamespacestd intmain sets s insert 999 s insert 18 s insert 321 s insert 999 set const iteratorit it s begin while it s end cout key it s find key if it s end cout key isnotinset endl elsecout key isinset endl return0 include include includeusingnamespacestd intmain mapm m zero 0 m one 1 m two 2 m three 3 m four 4 m five 5 m six 6 m seven 7 m eight 8 m nine 9 cout m three endl m five endl m seven endl return0 7 3 7容器适配器容器适配器是基本容器的衍生物 并利用基本容器来实现其特定的功能 容器适配器有三种 stack queue和priority queue 默认情况下STLstack衍生自dequestacks stack s 如果要改变成vector的衍生 则用如下方式 stack s include include includeusingnamespacestd intmain conststringprompt Enteranalgebraicexpression constcharlParen constcharrParen stacks stringbuf cout prompt endl getline cin buf for inti 0 i buf length i if isspace buf i s push buf i cout Originalexpression buf endl cout Expressioninreverse while s empty chart s top s pop if t lparen t rParen elseif t rParen t lParen cout t cout endl return0 include includeusingnamespacestd intmain queueq queque intj 1 for inti 0 i 6 i q push j j 2 while q empty cout q front endl returnintegerq pop returnvoid return0 include include include include includeusingnamespacestd intmain constintHOW MANY 8 inti priority queuenums srand time NULL for i 0 i HOW MANY i intnext rand cout next endl nums push next cout n Prioritybyvalue endl for i 0 i HOW MANY i cout nums top endl nums pop return0 7 3 8其他容器 string include include includeusingnamespacestd voidprintChar charc cout c intmain strings pele thegreatestever cout s s endl cout sinreverse for each s rbegin s rend printChar cout endl char where find s begin s end a cout a isthe where s begin 1 thcharin s endl random shuffle s begin s end cout safterarandomshuffle s endl where find s begin s end a cout a isthe where s begin 1 thcharin s endl sort s begin s end cout ssortedinascendingorder s endl return0 7 3 9STL算法排序和搜索 数值处理 集合运算 复制等 include include include includeusingnamespacestd voiddump inti couti2 intmain vectorv 10 generate v begin v end rand replace if v begin v end odd 0 sort v begin v end comp for each v begin v end dump return0 include includeusingnamespacestd voidprint constchar char int intmain constintLEN 27 constintMED LEN 2 charalph abcdefghijklmnopqrstuvwxyz print n nOriginalarray n alph LEN random shuffle alph alph LEN print Afterrandom shuffle n alph LEN nth element alph alph MED alph LEN print n nAfternth element n alph LEN print n tmedian alph MED 1 LEN 2 cout cout 7 3 10其他STL构件函数对象 functionobject 函数适配器 functionadaptor STLallocator 7 4异常处理voidg try f catch intx 整型异常被抛出的处理 catch chars 字符型的异常被抛出的处理 例strings intindex len while true cout index len try s erase index len catch out of range cou        
    温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 街道开展土蜂蜜活动方案
- 视频拍摄大型活动方案
- 衢州小型团建活动方案
- 跨年公司创意场馆活动方案
- 追踪幼儿游戏活动方案
- 街道办故事活动方案
- 2025年低空旅游「智慧出行」解决方案报告
- 2025年风能产业供应链智能化解决方案报告
- 2025年教师资格考试《学科教学内容与方法》备考题库及答案解析
- 货车从业考试试题及答案解析
- 高教社马工程伦理学(第二版)教学课件06
- 内河船舶保险年费率
- 《电影场景构图》课件
- 《种鸡场卫生管理》课件
- 《工业园区清洁生产审核指南》
- “职”引未来知到智慧树章节测试课后答案2024年秋云南师范大学
- 《IBM战略人才》课件
- 《城市道路水下隧道设计规范》
- 半导体材料行业报告:InP 磷化铟衬底
- 酒店客房服务与卫生标准
- 工程热力学(严家騄)课后答案
 
            
评论
0/150
提交评论