欢迎来到淘文阁 - 分享文档赚钱的网站! | 帮助中心 好文档才是您的得力助手!
淘文阁 - 分享文档赚钱的网站
全部分类
  • 研究报告>
  • 管理文献>
  • 标准材料>
  • 技术资料>
  • 教育专区>
  • 应用文书>
  • 生活休闲>
  • 考试试题>
  • pptx模板>
  • 工商注册>
  • 期刊短文>
  • 图片设计>
  • ImageVerifierCode 换一换

    c_stl范例大全.pdf

    • 资源ID:90594629       资源大小:9.75MB        全文页数:140页
    • 资源格式: PDF        下载积分:12金币
    快捷下载 游客一键下载
    会员登录下载
    微信登录下载
    三方登录下载: 微信开放平台登录   QQ登录  
    二维码
    微信扫一扫登录
    下载资源需要12金币
    邮箱/手机:
    温馨提示:
    快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
    如填写123,账号就是123,密码也是123。
    支付方式: 支付宝    微信支付   
    验证码:   换一换

     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    c_stl范例大全.pdf

    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);/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 ;cout 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;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 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 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”,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 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 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 Si 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;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 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 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 namespace 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.erase(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;);templatevoid 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;OUTPUT:/En ti re vecto r:/Li n da75000/Ro b ert60000/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(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 po 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);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 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()(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 ;)private: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 ttinclude Sinclude Sinclude Sinclude ttinclude using namespace std;class 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 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(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();co ut en dl;return 0;OUTPUT:/96 Sm i th A./91 Am stro n g B./82 Watso n D./So rted b y Sco re/=/82 Watso n D./91 Am stro n g B./96 Sm i th A./Reverse o utput/=/96 Sm i th A./91 Am stro n g B./82 Watso n D.reserveSi n cl ude Si n cl ude usi n g n am espace std;i n t m ai n ()(vecto r v(5,0);/5 el em en ts,each -val ue 0/*-*/co ut Si ze o f v=v.si ze()en dl;co ut ”Capaci ty v=v.capaci ty()en dl;co ut Val ue o f each el em en t i s-;f o r(i n t i =0;i v.si ze();i+)co ut vi ;co ut en dl;v0=5;/n ew val ue f o r f i rst el em en tvl =8;v.push _ b ack(3);/creates n ew(6th)el em en t o f vecto r,v.push _ b ack(7);/auto m ati cal l y i n creases si zeco ut en dl;/capaci ty o f vecto r vco ut ”Si ze o f v=v.si ze()en dl;co ut ”Capaci ty v=X v.capaci ty()en dl;co ut ”Val ue o f each el em en t i s-;f o r(i n t i =0;i v.si ze();i+)cout vi ;cout endl endl;v.reserve(100);/increase capacity to 100cout ”Size of vl_int=v.size 0 endl;cout ”Capacity vl_int=v.capacity()endl;int size=sizeof(v);/how big is vector itselfcout ”sizeof v=size endl;return 0;OUTPUT:/Size of v=5/Capacity v=5/Value of each element is-0 0 0 0 0/Size of v=7/Capacity v=10/Value of each element is-5800037/Size of v=7/Capacity v=100/sizeof v=12resizeSinclude#include Sinclude Sinclude using namespace std;int main()(vector v(5);for(int i=0;i5;i+)vi=i*2;copy(v.begin(),v.end(),ostream_iterator(cout,z,);cout endl;v.resize(7,100);copy(v.begin(),v.end(),ostream_iterator(cout,/z);cout endl;v.resize(4);copy(v.begin(),v.end(),ostream_iterator(c o u t,);cout endl;return 0;OUTPUT:/0 2 4 6 8/0 2 4 6 8 100 100/0 2 4 6sizeSinclude Sinclude#include ttinclude using namespace std;template class Print(public:void operator()(T&t)(cout t ;);=int main()(vector v(5);Print print;cout ”Size of v=zz v.size()endl;fill(v.begin(),v.end。,*);for_each(v.begin(),v.end(),print);cout endl;for(int i=0;i v.size();i+)cout vicout endl;for(int i=0;i5;i+)co ut ”Si ze o f v 二;f o r_ each(v.b eg i n(),v.en d(),pri n t);co ut en dl;v.po p b ack ();return 0;)OUTPUT:/Si ze o f v=*/Si ze o f v=/Si ze o f v=/Si ze o f v=/Si ze o f v=/Si ze o f v=5*swapWi 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 t(pub l i c:vo i d o perato r()(T&t)(co ut t ;);=i n t m ai n ()(i n t ary =1,2,3,4,5,6,7,8,9,10;Pri n t pri n t;vecto r vl (ary,ary+7);vecto r v2(ary+7,ary+10);co ut ”Vecto r vl :;f o r_ each (vl.b eg i n (),vl.en d(),pri n t);co ut en dl;cout ”Size of vl=vl.size()endl endl;cout ”Vector v2:;for_each(v2.begin(),v2.end(),print);cout endl;cout ”Size of v2=v2.size()endl endl;vl.swap(v2);cout ”After swapping/endl;cout ”Vector vl:;for_each(vl.begin(),vl.end(),print);cout endl;cout Size of vl=vl.size()endl endl;cout ”Vector v2:;for_each(v2.begin(),v2.end(),print);cout endl;cout ”Size of v2=v2.size()endl endl;return 0;)OUTPUT:/Vector vl:1 2 3 4 5 6 7/Size of vl=7/Vector v2:8 9 10/Size of v2=3/After swapping:/Vector vl:8 9 10/Size of vl=3/Vector v2:1 2 3 4 5 6 7/Size of v2=7Dequeconstructorsttinclude ttinclude Sinclude include using namespace std;int main()string s tr=Alex,“John,Robert;/empty deque objectdeque d l;/creates deque with 10 empty elementsdeque d2(10);/creates deque with 10 elements,/and assign value 0 for eachdeque d3(10,0);/creates deque and assigns/values from string arraydeque d4(str+0,str+3);deque:iterator sit=d4.begin();while(sit!=d4.en d()cout *slt+;cout endl;/copy constructordeque d5(d4);for(int i=0;i3;i+)cout d5i ;cout endl;return 0;)OUTPUT:/Alex John Robert/Alex John RobertassignSinclude Sinclude Sinclude ttinclude using namespace std;int main()(int ary=l,2,3,4,5);deque d;/assign to the the contains ofd.assign(ary,ary+5);copy(d.begin(),d.end(),ostream_iterator(cout,);cout endl;/replace d for 3 copies of 100d.assign(3,100);copy(d.begin(),d.end(),ostream_iterator(c o u t,);cout endl;return 0;OUTPUT:/1 2 3 4 5/100 100 100at#include ttinclude using namespace std;int main()(deque d(3,0);d0=100;d.at(1)=200;for(int i=0;i3;i+)cout d.at(i)cout endl;return 0;O UTPUT:100 200 0backSinclude Sinclude#include Sinclude using namespace std;templateclass Member(public:Member(T t,D d):name(t),sal(d)void print();private:T name;D sal;);templatevoid Member:p rin t()(cout name sal endl;)/=int main()(typedef Member M;deque d;d.push_back(M(Robert”,60000);d.push back(M (z,Linda,z,75000);deque:iterator It=d.begin();cout ”Entire deque:print();cout endl;cout ”Return from back()zz endl;d.back().print();return 0;OUTPUT:/Entire deque:/Robert 60000/Linda 75000/Return from back()/Linda 75000beginSinclude Sinclude Sinclude Sinclude using namespace std;int main()(deque d(5);iota(d.begin(),d.end(),1);deque:iterator It=d.begin();while(It!=d.end()cout *It+;cout endl;/third element of the dequeIt=d.begin()+2;cout *It endl;return 0;OUTPUT:/1 2 3 4 5/3clearttinclude ttinclude ttinclude using namespace 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 ()(deque d(10);Pri n t pri n t;f i l l (d.b eg i n(),d.en d(),5);co ut ”Deque d:;f o r_ each (d.b eg i n (),d.en d(),pri n t);co ut en dl;co ut ”Si ze o f d=zz d.si ze()en dl;co ut d.cl ear”en dl;d.cl ear 0 ;co ut ”Deque d:;f o r_ each (d.b eg i n (),d.en d(),pri n t);co ut en dl;co ut ”Si ze o f d=d.si ze()en dl;co ut ”Deque d i s ;d.em pty()?co ut :co ut n o tco ut ”em pty”en dl;return 0;)/Deque d:5 5 5 5 5 5 5 5 5 5/Si ze o f d=10/d.cl ear/Deque d:/Si ze o f d=0/Deque d i s em ptyem ptySi n cl ude Sinclude using namespace std;int main()(deque d;cout ”Deque is ;d.empty()?cout :cout notcout ”empty”endl;d.push_back(100);cout ”Deque is ;d.empty()?cout :cout notcout ”empty”endl;return 0;/Deque is empty/Deque is not emptyendSincludeSincludettincludettincludeusing namespace std;int main()(deque d(5);iota(d.begin(),d.end(),1);deque:iterator It=d.begin();while(It!=d.en d()cout *It+;cout endl;/last element of the dequeIt=d.end()-1;cout *It endl;return 0;)OUTPUT:/1 2 3 4 5/5eraseSinclude#include ttinclude Sinclude using namespace std;int main()(deque d(10);deque:iterator I t;for(int i=0;i10;i+)di=i+1;copy(d.begin(),d.end(),ostream _iterator(cout,);cout endl;It=d.begin()+2;/remove third elementd.erase(It);copy(d.begin(),d.end(),ostream_iterator(c o u t,);cout endl;It=d.begin0;/remove 2 elements from beginning fo dd.erase(It,It+2);copy(d.begin(),d.end(),ostream_iterator(cout,/z);cout endl;return 0;OUTPUT:/1 2 3 4/1 2 4 5/4 5 6 75 6 7 8 9 106 7 8 9 108 9 10frontSinclude include ttinclude ttinclude using namespace std;templateclass Memberpublic:Member(T t,D d):name(t),sal(d)void print();private:T name;D s a l;);tem platevoid Member:p rin t()(cout name sal endl;/=int main()(typedef Member M;deque d;d.push_back(M(Linda”,75000);d.push_back(M(z,Robert,/,60000);deque:ite ra to r It=d.begin();cout ”Entire deque:print();cout endl;cout ”Return from front()z,endl;d.front().print();return 0;OUTPUT:/Entire/Linda/Robert/Return/Lindadeque:7500060000from front()75000in sertSinclude Sinclude#include ttinclude using namespace std;template class Printpublic:void operator()(T&t)(cout t ;);/=int main()(int ary5;fill(ary,ary+5,1);deque d;deque:iterator It;Print print;copy(ary,ary+5,back_inserter(d);cout ”deque d:;for_each(d.begin(),d.end(),print);cout endl;It=d.begin();/insert value at the position Itcout ,zd.insert(It,5):;d.insert(It,5);for_each(d.begin(),d.end(),print);cout endl;/insert range ary+2-ary+5 at the positionIt=d.begin()+5;cout ,zd.insert(It,ary+2,ary+5:;d.insert(It,ary+2,ary+5);for_each(d.begin(),d.end(),print);cout endl;/insert 2 value of 20 at the positionIt=d.end()-2;cout d.insert(It,

    注意事项

    本文(c_stl范例大全.pdf)为本站会员(奔***)主动上传,淘文阁 - 分享文档赚钱的网站仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知淘文阁 - 分享文档赚钱的网站(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    关于淘文阁 - 版权申诉 - 用户使用规则 - 积分规则 - 联系我们

    本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

    工信部备案号:黑ICP备15003705号 © 2020-2023 www.taowenge.com 淘文阁 

    收起
    展开