c_stl范例大全.pdf
《c_stl范例大全.pdf》由会员分享,可在线阅读,更多相关《c_stl范例大全.pdf(140页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Sample of STLSTL范 例()容器部分Vector-1Deque-20List-38Set-66Multiset-88Map-98Multimap-113Stack-136Queue-138Priority_queue-139VectorconstructorsSincludeSincludeSincludeincludeusing namespace std;int main()string str=Alex,“John,Robert;/empty vector objectvector vl;/creates vector with 10vector v2(10);/creat
2、es 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 ;cout endl;re
3、turn 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;vecto r v;/assi g n to th e th e co n tai n s o fv.assi g n(ary,ary+5);co py(v.b eg i n (),v.en d(),o stream _ i terato r(co ut,z,);co ut en dl;/repl ace v
4、f o r 3 co pi es o f 100v.assi g n(3,100);co py(v.b eg i n (),v.en d(),o stream _ i terato r(c o u t,);co ut en dl;return 0;)OUTPUT:/1 2 3 4 5/100 100 100attti n cl ude tti n cl ude usi n g n am espace std;i n t m ai n ()(vecto r v(3,0);v0=100;v.at(1)=200;f o r(i n t i=0;i 3;i+)co ut v.at(i)co ut en
5、 dl;return 0;OUTPUT:/100 200 0b acktti n cl ude Si n cl 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;vector v;v.push_back(M(Robert”,
6、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 Sinclude ttinclude Sinclude
7、 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()(vector v(10);cout ”Size
8、of v 二 v.size()endl;cout ”Capacity of v=v.capacity()endl;v.resize(100);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/Capaci ty o f v=10/Af ter resi zi n g:/Si ze o f v=100/Capaci ty o f v=100cl eari n cl ude#i n cl ude S
9、i n cl ude usi n g n am espace std;tem pl ate cl ass Pri n t(pub l i c:vo i d o perato r()(T&t)(co ut t ;);=i n t m ai n ()(vecto r v(10);Pri n t pri n t;f i l l (v.b eg i n(),v.en d(),5);co ut ”Vecto r v:;f o r_ each (v.b eg i n (),v.en d(),pri n t);co ut en dl;co ut ”Si ze o f v=,z v.si ze()en dl;
10、co ut v.cl ear”en dl;v.cl ear 0 ;co ut ”Vecto r v:;f o r_ each (v.b eg i n (),v.en d(),pri n t);co ut en dl;co ut ”Si ze o f v=v.si zeO en dl;co ut ”Vecto r v i s ;v.em pty()?co ut :co ut n o tcout ”empty”endl;return 0;)/Vector v:5 5 5 5 5 5 5 5 5 5/Size of v=10/v.clear/Vector v:/Size of v=0/Vector
11、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 emptyendSinclude Sinclude Sinclude include using names
12、pace 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 namespace std;int main()(vector v(10);vector:iter
13、ator 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.erase(It,It+2);copy(v.begin(),v.end(),ostream_it
14、erator(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;);templatevoid Member:print()(cout name sal endl;
15、)=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;OUTPUT:/En ti re vecto r:/Li n da75000/Ro b e
16、rt60000/Return f ro m f ro n t()/Li n da75000i n sert#i n cl ude Si n cl ude Si n cl ude Si n cl ude usi n g n am espace std;tem pl ate cl ass Pri n tpub l i c:vo i d o perato r()(T&t)(co ut t ;);=i n t m ai n ()(i n t ary5;f i l l (ary,ary+5,1);vecto r v;vecto r:i terato r It;Pri n t pri n t;co py(
17、ary,ary+5,b ack i n serter(v);co ut ”vecto r v:;f o r_ each (v.b eg i n (),v.en d(),pri n t);co ut en dl;It=v.b eg i n ();/i n sert val ue 5 at th e po si ti o nco ut ,zv.i n sert(It,5):;v.i n sert(It,5);f o r_ each (v.b eg i n (),v.en d(),pri n t);co ut en dl;/i n sert ran g e ary+2-ary+5 at th e p
18、o si ti o nIt=v.b eg i n ()+5;co ut z,v.i n sert(It,ary+2,ary+5:;v.i n sert(It,ary+2,ary+5);f o r_ each (v.b eg i n (),v.en d(),pri n t);co ut en dl;/i n sert 2 val ue o f 20 at th e po si ti o nIt=v.en d()-2;co ut ,zv.i n sert(It,2,20):v.i n sert(It,2,20);f o r_ each (v.b eg i n (),v.en d(),pri n t
19、);co ut en dl;return 0;)OUTPUT:/vecto 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 cl ude Si n cl ude usi n g n am espace std;i n t m ai n ()vecto r v(10);co ut ”Si ze o f v v.si ze()en dl;co ut Max_ si
20、 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_ b ackSi n cl ude Sinclude Sinclude using namespace std;template class Print(public:void operator()(T&t)cout t ;);/=int main()vector v;Print print;for(int i=0;i5;i+)v.push_back(i+l);while(!v.empty()(fo
21、r_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 ;)private:T name;=int main()(typedef Name N;typedef vector V;V v;N nl(
22、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 ttinclude Sinclude Sinclude Sinclude ttinclude using namespace std;cl
23、ass IDf ri en d b o o l o perato r (co n st ID&,co n st ID&);pub l i c:ID(stri n g n am e,i n t sco re):n am e(n am e),sco re(sco re)vo i d di spl ay()(co ut.setf(i o s:l ef t);co ut setw(3)sco re n am e en dl;)pri vate:stri n g n am e;i n t sco re;);/-/co m perati o n f un cti o n f o r so rti n gb
24、 o o l o perato r (co n st ID&a,co n st ID&b )(return a.sco re b.sco re;/-typedef vecto r Vecto r;/n ew n am e f o r exi sti n g datatypei n t m ai n ()(Vecto r v;Vecto r:i terato r Iter;v.push _ b ack(ID(Sm i th A,96);v.push _ b ack (ID(z,Am stro n g B.,91);v.push _ b ack (ID(Z/Watso n D.,82);f o r
25、(Iter=v.b eg i n();Iter!=v.en d();Iter+)Iter-di spl ay();so rt(v.b eg i n(),v.en d0);/so rt al g o ri th mco ut en dl ”So rted b y Sco re”en dl;co ut /,=/di spl ay();co ut en dl ”Reverse o utput”en dl;co ut =en dl;Vecto r:reverse_ i terato r r=v.rb eg i n O;wh i l e(r!=v.ren d()co ut redi spl ay();c
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- c_stl 范例 大全
限制150内