泛型程序设计与CSTL简介.ppt

上传人:小飞机 文档编号:6305257 上传时间:2023-10-15 格式:PPT 页数:77 大小:452KB
返回 下载 相关 举报
泛型程序设计与CSTL简介.ppt_第1页
第1页 / 共77页
泛型程序设计与CSTL简介.ppt_第2页
第2页 / 共77页
泛型程序设计与CSTL简介.ppt_第3页
第3页 / 共77页
泛型程序设计与CSTL简介.ppt_第4页
第4页 / 共77页
泛型程序设计与CSTL简介.ppt_第5页
第5页 / 共77页
点击查看更多>>
资源描述

《泛型程序设计与CSTL简介.ppt》由会员分享,可在线阅读,更多相关《泛型程序设计与CSTL简介.ppt(77页珍藏版)》请在三一办公上搜索。

1、第十五章 泛型程序设计与C+STL简介,学习目标,掌握泛型程序设计思想以及概念;掌握STL中顺序容器的使用,了解关联容器;掌握迭代器的使用;了解泛型算法和函数对象。,2,目录,15.1 泛型程序设计的概念和术语15.1.1 泛型程序设计15.1.2 STL的相关概念和术语15.2 C+STL中的容器15.2.1 顺序容器15.2.2 关联容器15.2.3 容器适配器,3,目录(续),15.3 迭代器15.3.1 迭代器的分类15.3.2 迭代器适配器15.3.3 迭代器相关的辅助函数15.4 标准C+库中的算法简介15.4.1 非可变序列算法15.4.2 可变序列算法15.4.3 排序及相关算

2、法,4,目录(续),15.4 标准C+库中的算法简介15.4.4 数值算法15.5 函数对象15.5.1 函数对象15.5.2 函数适配器,5,15.1.1 泛型程序设计,泛型程序设计是继面向对象程序设计之后的又一种程序设计方法。泛型程序设计就是让程序写得通用,能够适用于各种数据类型与数据结构,并且并不损失程序效率。面向对象与泛型程序设计这两种程序设计方法并不矛盾,而是相得益彰。标准模板库(Standard Temp late L ibrary,简称STL)是建立在C+中模板机制上的泛型程序设计思想的实现。,6,15.1.2 标准模板库相关概念和术语,容器容器是存放其他对象的对象。比如我们常见

3、的C+内置数组,从广义上讲也属于一种容器。容器可以存放同一种类型的一组元素或对象,称为同类容器类(homogenous constainer);或者存放不同类型的的元素或对象时,称为异类容器类(heterogenous constainer)。对于STL容器库,其包含了两类容器,一种为顺序容器(sequence contsainer),另一种为关联容器(associative container)。迭代器在C+中,我们经常使用指针。而迭代器就是相当于指针,它提供了一种一般化的方法使得C+程序能够访问不同数据类型的顺序或者关联容器中的每一个元素,我们可以称它为“泛型指针”。STL定义了五种迭代器

4、类型,前向迭代器(forward iterator),双向迭代器(bidirectional iterator),输入迭代器(input iterator),输出迭代器(output iterator),随机访问迭代器(random access iterator)。,7,15.1.2 标准模板库相关概念和术语,算法算法是STL中的核心,它它包含了70多个通用算法。可以分为四类:不可变序列算法(non-modifying sequence algorithms)、可变序列算法(mutating sequence algorithms)、排序及相关算法(sorting and related a

5、lgorithms)和算术算法(numeric algorithms)。函数对象函数对象是STL提供的四种组件中的一种,它是定义了操作符【operator()】的对象。在C+中,除了定义了操作符operator()的对象之外,普通函数或者函数指针也满足函数对象的特征。结合函数模板的使用,函数对象使得STL更加灵活和方便,同时也使得代码更为高效。本章在15.5小节将单独介绍函数对象。,8,15.1.2 标准模板库相关概念和术语,适配器(adapter)适配器是一种接口类,可以认为是标准组件的改装。通过修改其它类的接口,使适配器满足一定需求,可分为容器适配器、迭代器适配器和函数对象适配器三种。分配

6、器(allocator)分配器是STL提供的一种内存管理类模块。每种STL容器都是用了一种分配器类,用来封装程序所用的内存分配模式的信息。不同的内存分配模式采用不同的方法从 操作系统中检索内存。分配器类可以封装许多方面的信息,包括指针、常量指针、引用、常量引用、对象大小、不同类型指针之间的差别、分配函数与释放函数、以及一些函数的信息。分配器上的所有操作都具有分摊常量的运行时间。,9,15.1.2 标准模板库相关概念和术语,容器接口STL为容器提供了一些公共接口,这些公共接口是通用的,也可以说是泛型的。这些都是容器设计的规范,对于容器而言,定义的公共接口主要有以下几个部分:,10,15.1.2

7、标准模板库相关概念和术语,容器接口所有容器定义的迭代器访问接口,11,15.1.2 标准模板库相关概念和术语,容器接口其他访问接口,12,15.2.1 顺序容器,顺序容器顺序容器包含Vector,deque和list三种容器,其中vector和deque属于直接访问容器,list属于顺序访问容器。向量(vector)向量相当于一个动态数组,其可以动态存储元素,并提供对容器元素的随机访问。为了提高效率,vector并不是随着每一个元素的插入而增长自己,而是当vector要增长自己的时候,他分配的空间比当前所需的空间要多一些。这多一些的内存空间使需要添加新元素的时候不必再重新分配内存。与C+的内置

8、数组相比较,除了动态之外,向量容器支持向对象。,13,例15-1:建立一个整型向量容器,#include#include/使用向量容器须包含的头文件#include using namespace std;const int n=5;int main()int arrayn=12,4,5,9,1;vector vec1;/构造1:定义一个空的整型向量容器vec1 int i;for(i=0;i vec2(vec1);/构造2:拷贝构造vec2 vector vec3(array,array+3);/构造3:用array到array+3的值初始化 vector vec4(n,3);/构造4:用n

9、个3初始化向量,14,例15-1(续),for(i=0;in;i+)coutsetw(5)vec1i;coutendl;for(i=0;in;i+)coutsetw(5)vec2i;coutendl;for(i=0;i3;i+)coutsetw(5)vec3i;coutendl;for(i=0;in;i+)coutsetw(5)vec4i;coutendl;,15,例15-1(续),运行结果:12 4 5 9 1 12 4 5 9 1 12 4 5 3 3 3 3 3,16,例15-2:向量容器中元素的添加和删除,#include#include#include#include using n

10、amespace std;void display(vector,17,例15-2(续),int main()string str3=Hello,C+,Love;vector vec1;/将str至str+3之间的元素插入vec1,vec1.insert(vec1.begin(),str,str+3);vector vec2;/将vec1.begin()至vec1.end()之间的元素插入到vec2,等效于将vec1复制到vec2中,vec2.insert(vec2.end(),vec1.begin(),vec1.end();vec2.insert(vec2.begin(),1,welcom

11、to C+);/在vec2.begin()前插入字符串display(vec1);display(vec2);vec1.clear();/清除整个vec1display(vec1);vec2.erase(vec2.begin();/删除vec2的首元素display(vec2);vec2.pop_back();/删除vec2的末尾元素display(vec2);,18,例15-2(续),运行结果:there are 3 elements in the vector.Hello C+Lovethere are 4 elements in the vector.welcom to C+Hello

12、C+Lovethere are 0 elements in the vector.there are 3 elements in the vector.Hello C+Lovethere are 2 elements in the vector.Hello C+,例15-3:向量容器中元素的添加和删除,#include#include#include#include using namespace std;int main()int array4=2,3,5,2;vector nval;coutnvals size is:nval.size()and the capacity is:nval.

13、capacity()endl;for(int i=0;i17;i+)nval.push_back(i);if(!(i%4)coutnvals size is:nval.size()and the capacity is:nval.capacity()endl;,20,例15-3(续),运行结果:nvals size is:0 and the capacity is:0nvals size is:1 and the capacity is:1nvals size is:5 and the capacity is:6nvals size is:9 and the capacity is:9nval

14、s size is:13 and the capacity is:13nvals size is:17 and the capacity is:19,例15-4:复杂对象的向量容器,#include#include#include#include using namespace std;const int n=6;struct Evaluate/结构体,string Name;/学生的姓名double Rank;/学生的排名;void display(vector,22,例15-4(续),int main()string namen=John,Lily,David,Jevons,Mike,Ja

15、ne;vector NameSet;vector rank(n,0);/初始化一个大小为n,元素都为0的向量NameSet.insert(NameSet.begin(),name,name+n);int i;for(i=0;iranki;vector Student;,23,例15-4(续),for(i=0;i Student2(Student);/调用向量的拷贝构造函数cout“the partial rank of Student2 set is”endl;display(Student);,24,例15-4(续),vector Student3(Student2.begin(),Stud

16、ent2.end()-3);cout“the size of Student3 is:”Student3.size()endl;/向量所含元素Student3.swap(Student);/交换两个容器coutthe size of Student is:Student.size()endl;/交换后的student的元素个数coutthe size of Student3 is:Student3.size()endl;/交换后的student3的元素个数Student3.pop_back();/删除最后一个元素coutsetw(5)Student3.size();Student3.erase

17、(Student3.begin();/删除最开始的一个元素coutsetw(5)Student3.size()endl;display(Student3);/显示更新后的向量容器,25,例15-4(续),运行结果:Please enter Johns rank(0100):1Please enter Lilys rank(0100):2Please enter Davids rank(0100):3Please enter Jevonss rank(0100):4Please enter Mikes rank(0100):5Please enter Janes rank(0100):6the

18、rank of Student set isJohns rank is 0.01Lilys rank is 0.02Davids rank is 0.03Jevonss rank is 0.04Mikes rank is 0.05,26,Janes rank is 0.06the partial rank of Student2 set isJohns rank is 0.01Lilys rank is 0.02Davids rank is 0.03Jevonss rank is 0.04Mikes rank is 0.05Janes rank is 0.06the size of Stude

19、nt3 is:3the size of Student is:3the size of Student3 is:6 5 4Lilys rank is 0.02Davids rank is 0.03Jevonss rank is 0.04Mikes rank is 0.05,15.2.1 顺序容器(续),双端队列(deque)双端队列是一种增加了访问权限的队列。在队列中,我们只允许从队列的一端添加元素,在队列的另一端提取元素;在双端队列中,其支持两端的出队和入队,这个我们可以通过前面所述的顺序容器接口看出。Vector与deque同属于随机访问容器,vector拥有的成员函数deque也都含有。

20、这个我们在顺序容器一般接口表中可以看出。列表(list)列表如我们在13章学习的链表一样,但是在13章中我们只实现了单链表,也就是只支持从一个方向遍历元素,STL实现的list由节点组成的双向链表,每个结点包含着一个元素,提供从两个方向遍历元素。,27,例15-5:建立一个双端队列容器,#include#include/使用deque需要包含的头文件#include using namespace std;const int n=10;int main()deque de;int arrayn=10,1,3,4,5,7,2,9,8,6;int i;for(i=0;i5;i+)de.push_b

21、ack(arrayi);de.push_front(arrayn-1-i);for(i=0;in;i+)coutsetw(5)dei;coutendl;for(i=0;in;i+)de.pop_front();de.push_back(arrayi);for(i=0;in;i+)coutsetw(5)dei;coutendl;,28,例15-5:建立一个双端队列容器,#include#include/使用deque需要包含的头文件#include using namespace std;const int n=10;int main()deque de;int arrayn=10,1,3,4,

22、5,7,2,9,8,6;int i;for(i=0;i5;i+)de.push_back(arrayi);de.push_front(arrayn-1-i);for(i=0;in;i+)coutsetw(5)dei;coutendl;for(i=0;in;i+)de.pop_front();de.push_back(arrayi);for(i=0;in;i+)coutsetw(5)dei;coutendl;,29,例15-5(续),运行结果:7 2 9 8 6 10 1 3 4 510 1 3 4 5 7 2 9 8 6,30,例15-6:建立一个列表,#include#include/使用l

23、ist需要包含的头文件#include using namespace std;const int n=10;void display(list _list)if(!_list.empty()list:iterator it;for(it=_list.begin();it!=_list.end();it+)coutsetw(5)*it;coutendl;elsecoutNull listendl;,31,例15-6:建立一个列表,int main()int arrayn=2,3,5,7,34,3,4,1,0,10;list list1;list1.insert(list1.begin(),ar

24、ray,array+n);display(list1);list list2=list1;for(int i=0;i7;i+)list2.remove(i);display(list2);list2.splice(list2.end(),list1);display(list2);display(list1);,32,例15-6(续),运行结果:2 3 5 7 34 3 4 1 0 10 7 34 10 7 34 10 2 3 5 7 34 3 4 1 0 10Null list,33,例15-7:列表中的算法,#include#include#include using namespace

25、std;const int n=5;void display(list _list)if(!_list.empty()list:iterator it;for(it=_list.begin();it!=_list.end();it+)coutsetw(5)*it;coutendl;elsecoutNull listendl;,34,例15-7:列表中的算法,int main()int arrayn=2,7,5,3,34;list list1;list1.insert(list1.begin(),array,array+n);display(list1);list1.sort(greater()

26、;/按降序排列,greater为降序函数对象 display(list1);list1.sort();/升序排列display(list1);list list2=list1;for(int i=0;i()display(list1);display(list2);list1.reverse();/逆序display(list1);,35,例15-7(续),运行结果:2 7 5 3 34 34 7 5 3 2 2 3 5 7 34 5 7 34 2 3 5 5 7 7 34 34Null list 34 34 7 7 5 5 3 2,36,15.2.2 关联容器,映射(map)映射提供了一个键

27、/值对,基于键的查询,迅速查找到键相对应的所需的值。如map map_name,其建立的是一个以Type1为索引,Type2的值的查询。集合(set)由节点组成的红黑树,每个节点都包含着一个元素,节点之间以某种作用于元素对的谓词排列,没有两个不同的元素能够拥有相同的次序。这个与map存储的结构是一致的,都是以二叉树结构的节点形式存放。,37,例15-8:建立一个学生学号与姓名的映射,#include#include#include/使用映射需要包含的头文件#include using namespace std;int main()map student;student2004010001=李

28、山;/直接插入元素typedef map:value_type valtype;/利用map中提供的键值对类型valtype(2004010002,刘丽);/建立一个键值对student.insert(valtype(2004010002,刘丽);/利用insert方法插入元素student.insert(valtype(2004010003,张斌);map:iterator it;/声明迭代器it=student.find(2004010003);/查找学号为2004010003的学生信息if(it!=student.end()/如果返回的是映射的结尾cout(*it).second;/则输

29、出该键对应的的值int count=student.count(2004010005);/查询是否存在2004010005/删除退学的学生信息student.erase(it=find(2004010001);,38,例15-8(续),运行结果:张斌,39,例15-9:建立快速查询的集合,#include#include/使用set需要包含的头文件#include using namespace std;int main()set Department;string str1(DPIM);string str=DME,DAE,DTE,DBE;Department.insert(“DCE”);f

30、or(int i=0;i:iterator it;it=Department.find(DAE);/if(it!=Department.end()cout*it endl;if(!Department.count(str1)Department.insert(str1);/for(it=Department.begin();it!=Department.end();it+)cout*it;coutendl;,40,例15-9(续),运行结果:DAEDAE DBE DCE DME DPIM DTE,41,15.2.3 容器适配器,容器适配器是通过修改调整容器的接口,使得容器适用于另一种不同效果。

31、修改顺序容器接口的容器适配器有stack和queue,其中stack是具有后进先出特性的访问受限的线性结构,而queue是具有先进先出特性的访问受限的线性结构,此外还有优先队列。栈容器适配器stack(栈)是一种容器适配器,前面已经讲过,它不是独立的容器,只是某种序列容器的变化,它提供原容器的一个专用的受限接口。缺省的stack类(定义在头文件中),是对deque(双端队列)的一种限制。队列容器适配器queue(队列容器)是另外一种容器适配器。它默认通过deque来实现队列,提供了如push,pop等成员函数,还包括测试队列的使用情况,元素个数,是否为空等等功能。,42,例15-10:简单的栈

32、应用,#include#include using namespace std;int main()stack sk1;for(int i=0;i10;i+)sk1.push(i);coutpop from the stack:;while(!sk1.empty()coutsk1.top();sk1.pop();,43,例15-10(续),运行结果:pop from the stack:9 8 7 6 5 4 3 2 1 0,44,例15-11:杨辉三角的显示,#include#include#include using namespace std;void Yanghui(int n);/输

33、出杨辉三角int main()Yanghui(10);void Yanghui(int n)queue q;q.push(1);/首先在队列中存第一行元素1int s=0;for(int i=0;i=n;i+)coutendl;/对于每一行输出换行coutsetw(n-i)*2);/设置输出格式q.push(0);/在每一行数据中间添加0for(int j=1;j=i+2;j+)/对于每一行的输出i+2项元素int t=q.front();/获取队首元素q.pop();q.push(s+t);/保存两项之和s=t;if(j!=i+2)coutsetw(4)s;/不打印i+2项的0,45,例15

34、-11(续),运行结果:1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1 1 8 28 56 70 56 28 8 1 1 9 36 84 126 126 84 36 9 1 1 10 45 120 210 252 210 120 45 10 1,46,15.3.1 迭代器的分类,输入迭代器input iterator迭代器可以用来读取容器中的元素,但是不能保证支持向容器写入操作。Inputiterator必须至少支持:两个迭代器的相等和不相等的判断(=,!=)、通过操作符(+)(前

35、置或者后置)使迭代器能向前递增指向下一个元素、通过指针操作符(*)完成对元素的读取,还有成员访问操作符(-)。输出迭代器output iterator 迭代器可以被认为是与输入迭代器相反功能的迭代器。它用来向容器中写写入元素,但是不保证支持读取容器的内容。支持的操作为至少需要包括操作符(+)(包括前置和后置)和指针操作符(*)(左值形式)。前向迭代器forward iterator 迭代器可以用来以一个方向遍历容器的元素,支持支持容器元素的读写。,47,15.3.1 迭代器的分类(续),双向迭代器bidirectional iterator迭代其可以用来从两个方向遍历容器的元素,支持容器元素的

36、读写。随机访问迭代器randomaccess iterator 迭代器支持容器元素的随机访问,同样支持容器元素的读与写。五类迭代器功能包含关系,48,例15-12:使用迭代器访问向量容器,#include#include using namespace std;const int N=7;void display(const vector,49,例15-12:使用迭代器访问向量容器,void display(const vector,50,例15-12(续),运行结果:2 3 5 2 8 18 42 3 5,51,15.3.2 迭代器适配器,迭代器适配器是通过修改调整迭代器的接口获得的适配器。

37、STL提供了两类迭代器适配器:逆向迭代器和插入型迭代器。逆向迭代器是一种适配器,他通过重新定义递增运算和递减运算,使其行为正好倒置。这样,使用这类迭代器算法将以逆向次序处理元素。所有标准容器都支持通过逆向迭代器来遍历元素。插入型迭代器插入型迭代器用来将赋值操作转换为插入操作。通过这种迭代器,算法可以执行插入行为而不是覆盖行为。,52,15.3.3 迭代器相关的辅助函数,STL为迭代器提供了三个辅助函数:advance()、distance()、iter_swap()。三个函数的原型如下:void advance(InputIterator,53,例15-13:辅助函数使用,#include#i

38、nclude#include using namespace std;const int N=7;int main()list iList;int arrayN=7,2,6,3,8,10,8;iList.insert(iList.begin(),array,array+N);list:iterator it=iList.begin();list:iterator itend=iList.end();int size=distance(it,itend);coutthe size of the list is:;coutsizeendl;advance(it,5);coutthe 6th ele

39、ment is:;cout(*it)endl;iter_swap(it,-iList.end();for(it=iList.begin();it!=itend;it+)cout(*it);coutendl;,54,例15-13(续),运行结果:the size of the list is:7the 6th element is:107 2 6 3 8 8 10,55,15.4 标准C+库中的算法简介,在STL中的算法都是通过C+模板实现的。容器是一个黑匣子,而算法也是一个黑匣子,迭代器可以认为是一跟导线。STL算法是通用的,每个算法都适合于若干不同的数据结构。STL标准算法可以分为四类。第一

40、类非可变序列算法,通常这类算法在对容器进行操作的时候不会改变容器的内容;第二类是可变序列算法,这类算法一般会改变所操作的容器的内容;第三类是排序以及相关的算法,包括排序和合并算法、二分查找算法、有序序列的集合操作算法;第四类算法是通用数值算法。,56,15.4.1 非可变序列算法,非可变序列算法可以修改所操作容器的元素。支持这类算法的迭代器的为输入迭代器和前向迭代器。共11个。,57,例15-14:可变序列算法实例,#include#include/算法需要包含的头文件#include/使用函数对象需要包含的头文件#include using namespace std;class outpu

41、blic:void operator()(int x)coutx;,58,例15-14(续),int main()int array=2,4,7,4,9,3,2,7,8;vector ivec(array,array+sizeof(array)/sizeof(int);vector:iterator iter;/找到第一个为4的元素位置,并输出此值和后面的所有值for(iter=find(ivec.begin(),ivec.end(),4);iter!=ivec.end();iter+)cout(),4)endl;/利用for_each算法输出每个元素for_each(ivec.begin()

42、,ivec.end(),out();coutendl;,59,例15-14(续),运行结果:4 7 4 9 3 2 7 8252 4 7 4 9 3 2 7 8,60,15.4.2 可变序列算法,可变序列算法可以修改他们所操作的容器的元素,这类算法28个。,61,15.4.2 可变序列算法(续),62,例15-15:可变序列算法实例,#include#include#include#include using namespace std;const int n=9;class square/定义平方操作的函数对象public:int operator()(int iter)return ite

43、r*iter;class odd_by_one/定义每次乘以2的函数对象public:int operator()()return x=x*2;private:static int x;int odd_by_one:x=1;,63,例15-15(续),int main()int arrayn=0,1,2,3,5,3,8,5,1;vector ivec1(n,0);vector:iterator iter=ivec1.begin();ostream_iterator output(cout,t);/定义输出流迭代器copy(array,array+n,output);/利用copy将数组内容考到

44、输出流对象中cout ivec2(array,array+n);generate(ivec2.begin(),ivec2.end(),odd_by_one();copy(ivec2.begin(),ivec2.end(),output);coutendl;,64,例15-15(续),/利用transform将ivec1中的每一个元素进行平方操作,并拷贝到ilist1中list ilist1(n,0);transform(ivec1.begin(),ivec1.end(),ilist1.begin(),square();copy(ilist1.begin(),ilist1.end(),outpu

45、t);cout:iterator it=remove(ilist1.begin(),ilist1.end(),9);ilist1.erase(it,ilist1.end();copy(ilist1.begin(),ilist1.end(),output);coutendl;/交换两个向量容器swap(ivec1,ivec2);,65,例15-15(续),运行结果:0 1 2 3 5 3 8 5 10 1 2 3 5 3 8 5 13 3 2 3 5 3 8 5 12 4 8 16 32 64 128 256 5129 9 4 9 25 9 64 25 14 25 64 25 1,66,15.4

46、.3 排序及相关算法,67,15.4.3 排序及相关算法(续),68,例15-16:排序算法举例,#include#include#include#include using namespace std;const int n=8;int main()int arrayn=4,3,7,2,2,3,8,5;vector ivec1(array,array+n);ostream_iterator output(cout,);/输出容器中最大和最小的元素cout*max_element(ivec1.begin(),ivec1.end()endl;cout*min_element(ivec1.begi

47、n(),ivec1.end()endl;/利用部分排序算法排序partial_sort(ivec1.begin(),ivec1.begin()+3,ivec1.end();copy(ivec1.begin(),ivec1.end(),output);coutendl;,69,例15-16(续),/部分排序,并将排序的拷贝到新的容器vector ivec2(5,0);partial_sort_copy(ivec1.begin(),ivec1.begin()+5,ivec2.begin(),ivec2.end();copy(ivec2.begin(),ivec2.end(),output);cou

48、tendl;/进行全区间的排序,默认为升序sort(ivec1.begin(),ivec1.end();copy(ivec1.begin(),ivec1.end(),output);coutendl;/利用二分法查找确定元素可插入的迭代器位置cout*lower_bound(ivec1.begin(),ivec1.end(),5)endl;cout*upper_bound(ivec1.begin(),ivec1.end(),5)endl;/利用合并算法将两个向量容器合并,并拷贝到新的数组中int array15+n;merge(ivec1.begin(),ivec1.end(),ivec2.b

49、egin(),ivec2.end(),array1);copy(array1,array1+13,output);coutendl;,70,例15-16(续),运行结果:822 2 3 7 4 3 8 52 2 3 4 72 2 3 3 4 5 7 8572 2 2 2 3 3 3 4 4 5 7 7 8,71,15.4.4 数值算法,数值算法包括4个算法,分别为accumulate(累积算法)、partial_sum(累加部分元素和算法)、adjacent_difference(相邻元素差)和inner_product(内积算法)。我们使用数值算法需要包含头文件。,72,例15-17:数值算

50、法实例,#include#include#include/所需要包含的头文件using namespace std;const int n=6;int main()int arrayn=2,2,1,5,3,6;vector ivec1(array,array+n);vector ivec2(ivec1);ostream_iterator output(cout,);/对序列进行求和累积coutaccumulate(ivec1.begin(),ivec1.end(),0)endl;/对序列进行部分求和partial_sum(ivec1.begin(),ivec1.end(),output);co

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号