c7模板和STL优质PPT.ppt

上传人:b****1 文档编号:14281548 上传时间:2022-10-21 格式:PPT 页数:49 大小:206.50KB
下载 相关 举报
c7模板和STL优质PPT.ppt_第1页
第1页 / 共49页
c7模板和STL优质PPT.ppt_第2页
第2页 / 共49页
c7模板和STL优质PPT.ppt_第3页
第3页 / 共49页
c7模板和STL优质PPT.ppt_第4页
第4页 / 共49页
c7模板和STL优质PPT.ppt_第5页
第5页 / 共49页
点击查看更多>>
下载资源
资源描述

c7模板和STL优质PPT.ppt

《c7模板和STL优质PPT.ppt》由会员分享,可在线阅读,更多相关《c7模板和STL优质PPT.ppt(49页珍藏版)》请在冰豆网上搜索。

c7模板和STL优质PPT.ppt

Arraya0(50);

Arraya1(50);

templateArraya2(50);

Arraya3(50);

7.1.2参数表中的模板类模板类可以作为一种数据类型出现在参数表中。

templateostream,7.2函数模板templateTMax(Tt,intn)inti=0;

Ttemp=t0;

for(i=1;

in;

i+)if(tempti)temp=ti;

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;

i10;

i+)v.push_back(i);

for_each(v.begin(),v.end(),sq);

intsq(inta)returna*a;

#include#include#includeusingnamespacestd;

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);

coutv1=;

for(v1_Iter=v1.begin();

v1_Iter!

=v1.end();

v1_Iter+)cout*v1_Iter;

coutendl;

coutv2=;

for(v2_Iter=v2.begin();

v2_Iter!

=v2.end();

v2_Iter+)cout*v2_Iter;

coutv4=;

for(v4_Iter=v4.begin();

v4_Iter!

=v4.end();

v4_Iter+)cout*v4_Iter;

coutv5=;

for(v5_Iter=v5.begin();

v5_Iter!

=v5.end();

v5_Iter+)cout*v5_Iter;

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;

inums.size();

i+)coutnumsiendl;

/15-99960coutendl;

nums.erase(nums.begin();

/-99960nums.erase(nums.begin();

/60for(i=0;

/60return0;

voiddump(list,names.reverse();

cout,#include#includeusingnamespacestd;

intmain()vectorc1;

iteratorc1_Iter;

const_iteratorc1_cIter;

c1.push_back

(1);

c1.push_back

(2);

c1_Iter=c1.begin();

coutThefirstelementofc1is*c1_Iterendl;

*c1_Iter=20;

c1_Iter=c1.begin();

coutThefirstelementofc1isnow*c1_Iterendl;

/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);

set:

const_iteratorit;

it=s.begin();

while(it!

=s.end()coutkey;

it=s.find(key);

if(it=s.end()coutkeyisnotinset.endl;

elsecoutkeyisinset.endl;

return0;

intmain()mapm;

mzero=0;

mone=1;

mtwo=2;

mthree=3;

mfour=4;

mfive=5;

msix=6;

mseven=7;

meight=8;

mnine=9;

coutmthreeendlmfiveendlmsevenendl;

7.3.7容器适配器容器适配器是基本容器的衍生物,并利用基本容器来实现其特定的功能。

容器适配器有三种:

stack、queue和priority_queue.默认情况下STLstack衍生自dequestacks;

stacks;

如果要改变成vector的衍生,则用如下方式:

intmain()conststringprompt=Enteranalgebraicexpression:

;

constcharlParen=(;

constcharrParen=);

stringbuf;

coutpromptendl;

getline(cin,buf);

ibuf.length();

i+)if(!

isspace(bufi)s.push(bufi);

coutOriginalexpression:

bufendl;

coutExpressioninreverse:

while(!

s.empty()chart=s.top();

s.pop();

if(t=lparen)t=rParen;

elseif(t=rParen)t=lParen;

coutt;

coutendl;

#include#includeusingnamespacestd;

intmain()queueq;

/=quequeintj=-1;

i6;

i+)q.push(j=j+2);

while(!

q.empty()coutq.front()endl;

/returnintegerq.pop();

/returnvoidreturn0;

#include#include#include#include#includeusingnamespacestd;

intmain()constintHOW_MANY=8;

inti;

priority_queuenums;

srand(time(NULL);

iHOW_MANY;

i+)intnext=rand();

coutnextendl;

nums.push(next);

coutn*Prioritybyvalue:

endl;

for(i=0;

i+)coutnums.top()endl;

nums.pop();

7.3.8其他容器(string)#include#include#includeusingnamespacestd;

voidprintChar(charc)coutc;

intmain()strings=“pele,thegreatestever”;

cout“s:

“sendl;

cout“sinreverse:

“;

for_each(s.rbegin(),s.rend(),printChar);

char*where=find(s.begin(),s.end(),a);

cout“aisthe“wheres.begin()+1“thcharin:

random_shuffle(s.begin(),s.end();

cout“safterarandomshuffle:

”sendl;

where=find(s.begin(),s.end(),a);

cout“aisthe”wheres.begin()+1“thcharin:

”sendl;

sort(s.begin(),s.end();

cout“ssortedina

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

当前位置:首页 > 高等教育 > 文学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1