C++STL范例大全教程.pdf
《C++STL范例大全教程.pdf》由会员分享,可在线阅读,更多相关《C++STL范例大全教程.pdf(140页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Sample of STLSTL范 例()容器部分Vec to r-1Deque-20Li st-38Set-66Mul ti set-88Map-98Mul ti m ap-113Stac k-136Queue-138Pri o ri tyqueue-139VectorconstructorsSincludeSincludeSincludeincludeusing namespace std;int main()string str=Alex,“John,Robert;/empty vector objectvector vl;/creates vector with 10vector v2
2、(10);/creates vector with 10/and assign value 0 forvector v3(10,0);empty elementselements,each/creates vector and assigns/values from string arrayvector v4(str+0,str+3);vector:iterator sit=v4.begin();while(sit!=v4.end()cout *slt+;cout endl;/copy constructorvector v5(v4);for(int i=0;i3;i+)cout v5i ;c
3、out endl;return 0;OUTPUT:/Alex John Robert/Alex John Robertassignincludettincludettincludettincludeusing namespace std;i n t m ai n ()(i n t ary =l,2,3,4,5;vec to r v;/assi g n to th e th e c o n tai n s o fv.assi g n(ary,ary+5);c o py(v.beg i n (),v.en d(),o stream _ i terato r(c o ut,z,);c o ut en
4、 dl;/repl ac e v fo r 3 c o pi es o f 100v.assi g n(3,100);c o py(v.beg i n (),v.en d(),o stream _ i terato r(c o u t,);c o ut en dl;return 0;)OUTPUT:/1 2 3 4 5/100 100 100attti n c l ude tti n c l ude usi n g n am espac e std;i n t m ai n ()(vec to r v(3,0);v 0=100;v.at(1)=200;fo r(i n t i=0;i 3;i+
5、)c o ut v.at(i)c o ut en dl;return 0;OUTPUT:/100 200 0bac ktti n c l ude Si n c l ude Sinclude Sinclude using namespace std;templateclass Member(public:Member(T t,D d):name(t),sal(d)void print();private:T name;D sa l;);templatevoid Member:print()(cout name sal endl;=int main()(typedef Member M;vecto
6、r v;v.push_back(M(Robert”,60000);v.push back(M (z,Linda,z,75000);vector:iterator It=v.begin();cout ”Entire vector/endl;while(It!=v.end()(It+)-print();cout endl;cout ”Return from back()endl;v.back().print();return 0;O UTPUT:/EntireRobertLindaReturnLindavector:6000075000from back()75000beginSinclude S
7、include ttinclude Sinclude using namespace std;int main()(vector v(5);iota(v.begin(),v.end(),1);vector:iterator It=v.begin();while(It!=v.end()cout *It+;cout endl;/third element of the vectorIt=v.begin()+2;cout *It endl;return 0;OUTPUT:/1 2 3 4 5/3capacityinclude#include using namespace std;int main(
8、)(vector v(1 0);cout ”Size of v 二 v.size()endl;cout ”Capacity of v=v.capacity()endl;v.resize(1 00);cout ”After resizing:endl;cout ”Size of v=z/v.size()endl;cout ”Capacity of v=v.capacity()endl;return 0;OUTPUT:/Si ze o f v=10/Capac i ty o f v=10/A fter resi zi n g:/Si ze o f v=100/Capac i ty o f v=10
9、0c l eari n c l ude#i n c l ude Si n c l ude usi n g n am espac e std;tem pl ate c l ass Pri n t(publ i c:vo i d o perato r()(T&t)(c o ut t ;);=i n t m ai n ()(vec to r v(10);Pri n t pri n t;fi l l (v.beg i n(),v.en d(),5);c o ut ”Vec to r v:;fo r_ eac h (v.beg i n (),v.en d(),pri n t);c o ut en dl;
10、c o ut ”Si ze o f v=,z v.si ze()en dl;c o ut v.c l ear”en dl;v.c l ear 0 ;c o ut ”Vec to r v:;fo r_ eac h (v.beg i n (),v.en d(),pri n t);c o ut en dl;c o ut ”Si ze o f v=v.si zeO en dl;c o ut ”Vec to r v i s ;v.em pty()?c o ut :c o ut n o tcout ”empty”endl;return 0;)/Vector v:5 5 5 5 5 5 5 5 5 5/Si
11、ze of v=10/v.clear/Vector v:/Size of v=0/Vector v is emptyemptySinclude Sinclude using namespace std;int main()vector v;cout ”Vector is;v.empty 0?cout :cout notcout ”empty”endl;v.push back(lOO);cout ”Vector is;v.empty()?cout :cout notcout ”empty”endl;return 0;)/Vector is empty/Vector is not emptyend
12、Sinclude Sinclude Sinclude include using namespace std;int main()vector v(5);iota(v.begin(),v.end(),1);vector:iterator It=v.begin();while(It!=v.en d()cout *It+;cout endl;/last element of the vectorIt=v.end()-1;cout *It endl;return 0;)OUTPUT:/1 2 3 4 5/5eraseSinclude#includettincludeWincludeusing nam
13、espace std;int main()(vector v(10);vector:iterator It;for(int i=0;i10;i+)vi=i+1;copy(v.begin(),v.end(),ostream _iterator(cout,);cout endl;It=v.begin()+2;/remove third elementv.erase(It);copy(v.begin(),v.end(),ostream_iterator(c o u t,);cout endl;It=v.begin0;/remove 2 elements from beginning fo vv.er
14、ase(It,It+2);copy(v.begin(),v.end(),ostream_iterator(cout,z,);cout endl;return 0;O UTPUT:/1 2 3 4 5 6 7 8 9 10/1 2 4 5 6 7 8 9 10/4 5 6 7 8 9 10frontttinclude Sinclude Sinclude#include using namespace std;templateclass Memberpublic:Member(T t,D d):name(t),sal(d)void print();private:T name;D sa l;);t
15、emplatevoid Member:print()(cout name sal endl;)=int main()(typedef Member M;vector v;v.push back(M(,zLindaz,,75000);v.push_back(M(Robert”,60000);vector:iterator It=v.begin();cout ”Entire vector:“endl;while(It!=v.end()(It+)-print();cout endl;cout ”Return from front(),z endl;v.front().print();return 0
16、;OUTPUT:/En ti re vec to r:/Li n da75000/Ro bert60000/Return fro m fro n t()/Li n da75000i n sert#i n c l ude Si n c l ude Si n c l ude Si n c l ude usi n g n am espac e std;tem pl ate c l ass Pri n tpubl i c:vo i d o perato r()(T&t)(c o ut t ;);=i n t m ai n ()(i n t ary 5;fi l l (ary,ary+5,1);vec
17、to r v;vec to r:i terato r It;Pri n t pri n t;c o py(ary,ary+5,bac ki n serter(v);c o ut ”vec to r v:;fo r_ eac h (v.beg i n (),v.en d(),pri n t);c o ut en dl;It=v.beg i n ();/i n sert val ue 5 at th e po si ti o nc o ut ,zv.i n sert(It,5):;v.i n sert(It,5);fo r_ eac h (v.beg i n (),v.en d(),pri n t
18、);c o ut en dl;/i n sert ran g e ary+2-ary+5 at th e po si ti o nIt=v.beg i n ()+5;c o ut z,v.i n sert(It,ary+2,ary+5:;v.i n sert(It,ary+2,ary+5);fo r_ eac h (v.beg i n (),v.en d(),pri n t);c o ut en dl;/i n sert 2 val ue o f 20 at th e po si ti o nIt=v.en d()-2;c o ut ,zv.i n sert(It,2,20):v.i n se
19、rt(It,2,20);fo r_ eac h (v.beg i n (),v.en d(),pri n t);c o ut en dl;return 0;)OUTPUT:/vec to r v/v.i n sert(It,5)/v.i n sert(It,ary+2,ary+5/v.i n sert(It,2,20)1 1 1 1 15 1 1 1 1 15 1 1 1 1 1 1 1 15 1 1 1 1 1 1 20 20 1 1m ax_ si zetti n c l ude Si n c l ude usi n g n am espac e std;i n t m ai n ()ve
20、c to r v(10);c o ut ”Si ze o f v v.si ze()en dl;c o ut Max_ si ze o f v 二 v.m ax_ si ze 0 en dl;return 0;)OUTPUT:/Si ze o f v=10/Max_ si ze o f v=1073741823po p_ bac kSi n c l ude Sinclude Sinclude using namespace std;template class Print(public:void operator()(T&t)cout t ;);/=int main()vector v;Pri
21、nt print;for(int i=0;i5;i+)v.push_back(i+l);while(!v.empty()(for_each(v.begin(),v.end(),p rin t);cout endl;v.pop_back();return 0;OUTPUT:/1 2 3 4 5/1 2 3 4/1 2 3/1 2/1push_backSincludeSincludeincludettincludeusing namespace std;template class Namepublic:Name(T t):name(t)void print()(cout name ;)priva
22、te:T name;=int main()(typedef Name N;typedef vector V;V v;N nl(Robert);N n2(Alex);v.push_back(nl);v.push_back(n2);/unnamed object of the type Namev.push_back(NC,Linda,/);V:iterator It=v.begin0;while(It!=v.end()(It+)-print();cout endl;return 0;)O UTPUT:/Robert Alex Lindarbegin and rend#include ttincl
23、ude Sinclude Sinclude Sinclude ttinclude using namespace std;class IDfri en d bo o l o perato r (c o n st ID&,c o n st ID&);publ i c:ID(stri n g n am e,i n t sc o re):n am e(n am e),sc o re(sc o re)vo i d di spl ay()(c o ut.setf(i o s:l eft);c o ut setw(3)sc o re n am e en dl;)pri vate:stri n g n am
24、 e;i n t sc o re;);/-/c o m perati o n fun c ti o n fo r so rti n gbo o l o perato r (c o n st ID&a,c o n st ID&b)(return a.sc o re b.sc o re;/-typedef vec to r Vec to r;/n ew n am e fo r exi sti n g datatypei n t m ai n ()(Vec to r v;Vec to r:i terato r Iter;v.push _ bac k(ID(Sm i th A,96);v.push _
25、 bac k(ID(z,A m stro n g B.,91);v.push _ bac k(ID(Z/Watso n D.,82);fo r(Iter=v.beg i n();Iter!=v.en d();Iter+)Iter-di spl ay();so rt(v.beg i n(),v.en d0);/so rt al g o ri th mc o ut en dl ”So rted by Sc o re”en dl;c o ut /,=/di spl ay();c o ut en dl ”Reverse o utput”en dl;c o ut =en dl;Vec to r:reve
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- STL 范例 大全 教程
限制150内