《The C++ Programming Language》课程教学资源(PPT课件讲稿)Lecture 03 Standard Template Library & Generic Programming

The c ++ Programming Language Lecture 3: Standard Template Library Generic Programming
The C++ Programming Language Lecture 3: Standard Template Library & Generic Programming

Brief introduction
Brief Introduction

Generic Programming Core idea Data structures and data types should be independent Data structures and algorithms should be independent Algorithms and data types should be independent (generic STL supports GP well Including 2 parts Container: vector, list, map, set..(basic data structures Generic Algorithm: find, sort, merge..(frequently used operations
Generic Programming ◼ Core idea ◼ Data structures and data types should be independent ◼ Data structures and algorithms should be independent ◼ Algorithms and data types should be independent (generic) ◼ STL supports GP well ◼ Including 2 parts ◼ Container: vector, list, map, set,… (basic data structures) ◼ Generic Algorithm: find, sort, merge,… (frequently used operations)

Generic Programming(cont u Container supported the independence between data types and data structures list, list Iterators supported the independence between data structures and algorithms sort(vec, begin, vec. endO; Function templates supported the independence between algorithms and data types template &, ElemType&)
Generic Programming (cont.) ◼ Container supported the independence between data types and data structures ◼ list, list ◼ Iterators supported the independence between data structures and algorithms ◼ sort(vec.begin(), vec.end()); ◼ Function templates supported the independence between algorithms and data types ◼ template binary_search(const vector&, ElemType&);

From Pointers to iterators
From Pointers to Iterators

From pointers to iterators J Request 1 u Given a vector of integer and an integer value, implement a findo function that if the value exists n vector, return a pointer to it, return 0 otherwise int* find(const vector& vec, int ivalue) for (int iX= 0; iX< vecsize; iX++) if (vec[iX]== iValue) return &vec[iXJH } return o
From pointers to iterators ◼ Request 1: ◼ Given a vector of integer and an integer value, implement a find() function that if the value exists in vector, return a pointer to it, return 0 otherwise int* find(const vector& vec, int iValue) { for (int iX = 0; iX < vec.size(); iX++) { if (vec[iX] == iValue) { return &vec[iX]; } } return 0; }

From pointers to iterators(cont,) J Request 2 u Let the previous findo function support any data types that having test equivalence operator(==) defined template ElemType* find( const vector &vec, const ElemType &value) for (int iX= 0; iX vec size; iX++) if (vec[iX]== Value) return &vec[]; return o
From pointers to iterators (cont.) ◼ Request 2: ◼ Let the previous find() function support any data types that having test equivalence operator (==) defined template ElemType* find(const vector &vec, const ElemType &Value) { for (int iX = 0; iX < vec.size(); iX++) { if (vec[iX] == Value) { return &vec[iX]; } } return 0; }

From pointers to iterators(cont,) Request 3 Let the previous findo function also support data elements in arrays Another overloaded function may solve it, but currently we try to use a single implementation Design philosophy: Divide and Conquer Pass the elements in array to findo, but never let the function be aware of the form of array a Pass the elements in vector to findo, but never let the function be aware of the form of vector
From pointers to iterators (cont.) ◼ Request 3: ◼ Let the previous find() function also support data elements in arrays ◼ Another overloaded function may solve it, but currently we try to use a single implementation ◼ Design philosophy: Divide and Conquer ◼ Pass the elements in array to find(), but never let the function be aware of the form of array ◼ Pass the elements in vector to find(), but never let the function be aware of the form of vector

From pointers to iterators(cont,) u For array, common ways are using pointers and subscription template ElemType* find(const ElemType* array int iSize, const ElemType &value) if((! array)I size 1) return 0 for(int iX= O; iX iSize; iX++) if (array[] = Value) return &array; return OF
From pointers to iterators (cont.) ◼ For array, common ways are using pointers and subscription template ElemType* find(const ElemType* array , int iSize, const ElemType &Value) { if ((! array) || size < 1) return 0; for (int iX = 0; iX < iSize; iX++) { if (array[iX] == Value) { return &array[iX]; } } return 0; }

From pointers to iterators(cont,) a better implementation adding a sentinel pointer that pointing to the next address of the last element, which could replace the size parameter using pointer dereference to replace subscription template ElemType* find(const Elem Type* first, const ElemType* last, const ElemType &value) if((! first)II(! last)) return O; for (i first != last; first++) if first = Value) return first } return OF }
From pointers to iterators (cont.) ◼ A better implementation: ◼ adding a sentinel pointer that pointing to the next address of the last element, which could replace the size parameter ◼ using pointer dereference to replace subscription template ElemType* find(const ElemType* first , const ElemType* last , const ElemType &Value) { if ((! first) || (! last)) return 0; for (; first != last; first++) { if (*first == Value) return first; } return 0; }
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 计算机问题求解(PPT讲稿)图的计算机表示以及遍历.pptx
- 系统软件与软件安全(PPT讲稿)构造安全、高效的系统软件.pptx
- 中国科学技术大学:《计算机体系结构》课程教学资源(PPT课件讲稿)第3章 流水线技术.ppt
- 合肥学院:《数据库原理与应用》课程教学资源(PPT课件)第4章 数据库的创建与管理.ppt
- 四川大学:《数据库技术》课程教学资源(PPT课件讲稿)第9章 数据库系统开发工具VB.ppt
- 上海交通大学:IT项目管理(PPT讲稿)讲座6 软件项目工作量估算.ppt
- 《操作系统》课程PPT教学课件(英文)内存管理 Memory Management.ppt
- 湖南生物机电职业技术学院:《电子商务概论》课程教学资源(PPT课件)第八章 电子商务安全.ppt
- 电子科技大学:《计算机操作系统》课程教学资源(PPT课件讲稿)第四章 设备管理 Device Management and Disk Scheduling.ppt
- 南京大学:模型检测(PPT课件讲稿)Model Checking.pptx
- 《计算机网络》课程教学大纲 Computer Networks.pdf
- 中国科学技术大学:《Linux操作系统分析》课程教学资源(PPT课件讲稿)Linux的进程(1/3).ppt
- 合肥工业大学:《数据库系统概论》课程教学资源(PPT课件)第四章 并发控制.ppt
- Phase Change Memory Aware Data Management and Application.pptx
- 《高级程序语言》课程教学资源(PPT课件讲稿)第09章 平台无关语言.ppt
- 并行算法 Parallel Algorithms(PPT讲稿)现状与展望 status and prospects.ppt
- 上海交通大学:Network Coding for Wireless Networks(PPT讲稿).pptx
- 西安电子科技大学:《现代密码学》课程教学资源(PPT课件讲稿)第七章 密码协议.pptx
- 北京大学:网络搜索引擎原理(PPT讲稿)Web Graph & Link Analysis.ppt
- 《数据结构 Data Structure》课程教学资源(PPT课件讲稿)第二章 线性表.ppt
- Scanning Electron Microscopy(SEM).ppt
- 《C语言程序设计》课程教学资源(PPT课件)第6章数据类型和表达式.ppt
- 面向对象编程 Object-Oriented Programming(PPT课件讲稿)继承 Inheritance.ppt
- 《单片机应用技术》课程PPT教学课件(C语言版)第7章 定时器/计数器.ppt
- 清华大学:《计算机导论》课程电子教案(PPT教学课件)第8章 计算机领域的典型问题.ppt
- 《网站设计与建设 Website design and developments》课程教学资源(PPT课件讲稿)第三部分 网站设计技术 第10章 HTML基础.ppt
- 山东大学:《面向对象程序设计》课程教学资源(PPT课件讲稿)第四章 编写对象接口.ppt
- 中国科学技术大学:《机器学习》课程PPT教学课件(讲稿)第二章 模型评估与选择.pptx
- 《C语言程序设计》课程电子教案(PPT课件)第三章 控制语句.ppt
- 安徽理工大学:《计算机网络》课程PPT教学课件(第4版)第1章 概述(编著:谢希仁).ppt
- 中国人民大学:《数据库系统概论 An Introduction to Database System》课程教学资源(PPT课件讲稿)第九章 关系查询处理和查询优化.ppt
- 《人工智能技术导论》课程教学资源(PPT课件讲稿)第8章 不确定性知识的表示与推理.ppt
- 福建工程学院:《C#程序设计》课程教学资源(实验指导书).doc
- 《计算机网络技术》课程教学资源(PPT课件讲稿)Chapter 03 物理层.ppt
- 沈阳理工大学:《网站建设与维护》课程教学资源(PPT课件讲稿)第四章 动态网页基础.ppt
- 东南大学:《操作系统概念 Operating System Concepts》课程教学资源(PPT课件讲稿)13 文件系统 I/O Systems.ppt
- 《C语言程序设计》课程教学资源(PPT课件讲稿)第6章 函数.ppt
- 《高级语言程序设计》课程教学资源(试卷习题)试题一(无答案).doc
- 中国科学技术大学:《密码学导论》课程教学资源(PPT课件讲稿)第4章 数论基础(主讲:李卫海).pptx
- 香港科技大学:Cross-Selling with Collaborative Filtering(PPT讲稿).ppt