- 2
- 0
- 约3.3千字
- 约 57页
- 2021-11-30 发布于安徽
- 举报
Lecture 13. C++ Standard Template Library;Outline;你有过这种经历吗?
在完成作业时,总要用到数组(Array)或链表(List),
总要写代码对其维护(增、删、查、改)!
“每次都重新发明一个轮子”
在整个软件领域,数十年确实都在为了一个目标而奋斗——可复用性(Reusability)
;Introduction;泛型程序设计:
将算法从特定的数据结构中抽象出来,变得更为通用
C++的模板为泛型程序设计奠定了关键的基础
STL是泛型程序设计的一个范例
容器(container)
迭代器(iterator)
算法(algorithms)
函数对象(function object);Outline;Containers;Containers ; ; ;关联容器类型;Sequential Containers;Vector Container;Vector;Slide 13- 15;Operation Support;Vector Container;Vector Container;Constructors for Vector;Outline;STL has containers, algorithms and Iterators
Containers hold objects, all of a specified type
Generic algorithms act on objects in containers
Iterators provide access to objects in the containers yet hide the internal structure of the container
;Using Declarations;Iterator Basics;vectorint
;Iterators;Iterators;Basic Iterator Operations;More Iterator Operations;Iterator Categories;Iterator Types Supported by Containers;Slide 13- 31;Bidirectional iterators provide the basic operations and the -- operators (pre- and postfix) to move to the previous data item.
双向迭代器可以自增和自减,无法使用某些容器(如单向链表)。
Example:
//从尾到头遍历容器vector中的所有元素
for ( vectorint::iterator it = c.end()-1; it != c.begin()-1; it-- )
cout *it “ ” ;
;Random access iterators
像访问数组一样来访问容器
Provide the basic operations and ––
Indexing p[2] returns the third element in the container
//遍历容器vector的对象 c 中的所有元素
for (int i = 0; i c.size(); i++)
cout c[i] ;
Iterator arithmetic p + 2 returns an iterator to the third element in the container
;Constant and Mutable Iterators;Constant and Mutable Iterators;Reverse Iterators;Example : 正向和逆向遍历vector中的所有元素
cout using iterator: ;
for ( vectorint::iterator it = c.begin(); it != c.end(); it++ )
cout *it ;
cout endl ;
cout using reverse_iterator: ;
for ( vectorint::reverse_iterator it = c.rbegin(); it != c.rend(); it-- )
cout *it ;
cout endl ;;Iterators;Iterators;Iterators;For_Each() Algorithm;Find() Algor
您可能关注的文档
最近下载
- 2023年辽宁何氏医学院临床医学《药理学》科目期末试卷B(有答案).docx VIP
- 山西省阳泉市2025-2026学年第一学期期中检测八年级英语试卷含答案.pdf
- 【西门子】A02_828D铣削编程简明教程_07-铣削循环.pdf VIP
- 2023年辽宁何氏医学院中西医临床医学《药理学》科目期末试卷A(有答案).docx VIP
- 医务人员常见心理健康问题与自我调节专家讲座.pptx VIP
- 临时用地不可避让耕地和永久基本农田的论证报告.docx
- 2025年房地产经纪人交易文件云存储与管理工具专题试卷及解析.pdf VIP
- 《金融市场与金融机构基础课后答案》.pdf VIP
- 2026年江苏旅游职业学院单招职业适应性考试模拟测试卷最新.docx VIP
- 2025年互联网营销师CRM系统GDPR与国内法规适配专题试卷及解析.pdf VIP
原创力文档

文档评论(0)