C++STL范例大全教程_第1頁
C++STL范例大全教程_第2頁
C++STL范例大全教程_第3頁
C++STL范例大全教程_第4頁
已閱讀5頁,還剩115頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

SampleofSTLSTL范例(一)

容器部分TOC\o"1-5"\h\zVector 1Deque 20\o"CurrentDocument"List 38\o"CurrentDocument"Set 66\o"CurrentDocument"Multiset 88\o"CurrentDocument"Map 98\o"CurrentDocument"Multimap 113Stack 136Queue 138Priorityqueue 139VectorconstructorsSinclude<iostream>Sinclude<vector>ttinclude<string>#include<algorithm>usingnamespacestd;intmain()(stringstrロ={"Alex","John","Robert"};//emptyvectorobjectvector<int>vl;//createsvectorwith10emptyelementsvector<int>v2(10);//createsvectorwith10elements,//andassignvalue0foreachvector<int>v3(10,0);//createsvectorandassigns//valuesfromstringarrayvector<string>v4(str+0,str+3);vector<string>::iteratorsit=v4.begin();while(sit!=v4.end())cout?*slt++くく””;cout?endl;//copyconstructorvector<string>v5(v4);for(inti=0;i<3;i++)cout?v5[i]?"";cout?endl;return0;}OUTPUT://AlexJohnRobert//AlexJohnRobertassign#include<iostream>ttinclude<vector>ttinclude<algorithm>ttinclude<iterator>usingnamespacestd;intmain()(intaryロ={1,2,3,4,5);vector<int>v;//assigntothe'v〃thecontainsof〃ary〃v.assign(ary,ary+5);copy(v.begin(),v.end(),ostream_iterator<int>(cout,z,zz));cout?endl;//replacevfor3copiesof100v.assign(3,100);copy(v.begin(),v.end(),ostream_iterator(cout,zz〃));cout<<endl;return0;)OUTPUT://12345//100100100at^include<iostream>ttinclude<vector>usingnamespacestd;intmain()(vector<int>v(3,0);v[0]=100;v.at(1)=200;for(inti=0;i<3;i++)cout?v.at(i)<<”cout?endl;return0;)OUTPUT://1002000backttinclude<iostream>#include<vector>Sinclude<string>Sinclude<iterator>usingnamespacestd;template<classT,classD>classMember{public:Member(Tt,Dd):name(t),sal(d){}voidprint();private:Tname;Dsal;};template<classT,classD>voidMember::print(){cout?name?”"?sal<<endl;intmain(){typedefMember<string,double>M;vector<M>v;v.push_back(M("Robert”,60000));v.push_back(M("Linda",75000));vector<M>::iteratorIt=v.begin();cout?"Entirevector:"?endl;while(It!=v.end())(It++)ー>print();cout<<endl;cout?"Returnfromback()"?endl;v.back().print();return0;)OUTPUT://Entirevector://Robert60000//Linda75000////Returnfromback()//Linda75000beginSinclude<iostream>#include<vector>ttinclude<iterator>#include<numeric>usingnamespacestd;intmain()(vector<int>v(5);iota(v.begin(),v.end(),1);vector<int>::iteratorIt=v.begin();while(It!=v.end())cout?*It++?〃”;cout?endl;//thirdelementofthevectorIt=v.begin()+2;cout?*It<<endl;return0;)OUTPUT://12345//3capacitySinclude<iostream>Sinclude<vector>usingnamespacestd;intmain(){vector<int>v(10);cout?”Sizeofv="?v.size()?endl;cout?"Capacityofv="?v.capacity()?endl;v.resize(100);cout?"Afterresizing:"<<endl;cout?"Sizeofv="?v.size()?endl;cout?"Capacityofv="?v.capacity()?endl;return0;OUTPUT://Sizeofv=10//Capacityofv=10//Afterresizing://Sizeofv=100//Capacityofv=100clearttinclude<iostream>Sinclude<vector>Sinclude<algorithm>usingnamespacestd;template<classT>classPrint{public:voidoperator()(T&t)(cout?t<<“}};intmain()(vector<int>v(10);Print<int>print;fill(v.begin(),v.end(),5);cout?,Vectorv:for_each(v.begin(),v.end(),print);cout?endl;cout?”Sizeofv="?v.size()?endl;cout?"v.clear"<<endl;v.clear();cout?"Vectorv:";for_each(v.begin(),v.end(),print);cout?endl;cout?"Sizeofv="くくv.sizeO?endl;cout?"Vectorvis";v.empty()?cout?"":cout?"notcout?”empty”<<endl;return0;)//Vectorv:5555555555//Sizeofv=10//v.clear//Vectorv://Sizeofv=0//VectorvisemptyemptySinclude<iostream>#include<vector>usingnamespacestd;intmain()(vector<int>v;cout?"Vectoris";v.empty()?coutくく:cout?"notcout?"empty"<<endl;v.push_back(100);cout?"Vectoris";v.empty()?coutくく:cout?"notcout?"empty"<<endl;return0;)//Vectorisempty//VectorisnotemptyendSinclude<iostream>Sinclude<vector>ttinclude<iterator>#include<numeric>usingnamespacestd;intmain()vector<int>v(5);iota(v.begin(),v.end(),1);vector<int>::iteratorIt=v.begin();while(It!=v.end())cout?*It++?”工cout?endl;//lastelementofthevectorIt=v.end()-1;cout?*It<<endl;return0;)OUTPUT://12345//5erasettinclude<iostream>#include<vector>ttinclude<iterator>#include<algorithm>usingnamespacestd;intmain()(vector<int>v(10);vector<int>::iteratorIt;for(inti=0;i<10;i++)v[i]=i+1;copy(v.begin(),v.end(),ostream_iteratorくint>(cout,〃〃));cout<<endl;It=v.begin()+2;//removethirdelementv.erase(It);copy(v.begin(),v.end(),ostream_iteratorくint>(cout,〃〃));cout?endl;It=v.begin();//remove2elementsfrombeginningfovv.erase(It,It+2);copy(v.begin(),v.end(),ostream_iteratorくint>(cout, 〃));cout?endl;return0;OUTPUT://12345678910//1245678910//45678910frontttinclude<iostream>Sinclude<vector>ttinclude<string>#include<iterator>usingnamespacestd;template<classT,classD>classMember(public:Member(Tt,Dd):name(t),sal(d){}voidprint();private:Tname;Dsal;};template<classT,classD>voidMember::print()(cout?name?”"?sal<<endl;)〃ニニ=================intmain()(typedefMember<string,double>M;vector<M>v;v.push_back(M("Linda”,75000));v.push_back(M("Robert”,60000));vector<M>::iteratorIt=v.begin();cout?”Entirevector:“?endl;while(It!=v.end())(It++)ー)print();cout?endl;cout?"Returnfromfront()"<<endl;v.front().print();return0;)OUTPUT://Entirevector://Linda75000//Robert60000////Returnfromfront()//Linda75000insertSinclude<iostream>#include<vector>Sinclude<iterator>#include<algorithm>usingnamespacestd;template<classT>classPrint(public:voidoperator()(T&t)(cout<<t?〃));〃==============intmain()(intary[5];fill(ary,ary+5,1);vector<int>v;vector<int>::iteratorIt;Print<int>print;copy(ary,ary+5,back_inserter(v));cout?”vectorv :";for_each(v.begin(),v.end(),print);cout?endl;It=v.begin();//insertvalue〃5〃attheposition〃It〃coutくくv.insert(It,5) :";v.insert(It,5);for_each(v.begin(),v.end(),print);

cout?endl;//insertrangeary+2-ary+5attheposition〃Il〃It=v.begin()+5;cout?z/v.insert(It,ary+2,ary+5:v.insert(It,ary+2,ary+5);for_each(v.begin(),v.end(),print);cout?endl;//insert2valueof"20〃attheposition"It"It=v.end()-2;cout?"v.insert(It,2,20) :";v.insert(It,2,20);for_each(v.begin(),v.end(),print);cout<<endl;return0;)return0;)OUTPUT://vectorv//v.insert(It,5)//v.insert(It,ary+2,ary+5//v.insert(It,2,20)111115111115111111115111111202011max_sizettinclude<iostream>#include<vector>usingnamespacestd;intmain(){vector<int>v(10);cout<<"Sizeofv?v.size()?endl;cout?"Max_sizeofv="?v.max_size()?endl;return0;}OUTPUT://Sizeofv=10//Max_sizeofv=1073741823pop_back#include<iostream>Sinclude<vector>Sinclude<algorithm>usingnamespacestd;template<classT>classPrint{public:voidoperator()(T&t)(cout?t?""?ヽ));〃=====================intmain()(vector<int>v;Print<int>print;for(inti=0;i<5;i++)v.push_back(i+l);while(!v.empty())(for_each(v.begin(),v.end(),print);cout?endl;v.pop_back();)return0;)OUTPUT://12345//1234//123//12//1push_backSinclude<iostream>ttinclude<vector>#include<string>#include<iterator>usingnamespacestd;template<classT>classNamepublic:Name(Tt):name(t){}voidprint()(cout?name?〃〃;)private:Tname;);〃=============================intmain()(typedefName<string>N;typedefvector<N>V;Vv;Nnl("Robert");Nn2("Alex");v.push_back(nl);v.push_back(n2);//unnamedobjectofthetypeNamev.push_back(N(〃Linda"));V::iteratorIt=v.begin();while(It!=v.end())(It++)ー>print();cout?endl;return0;)OUTPUT://RobertAlexLindarbeginandrend#include<iostream>ttinclude<iomanip>#include<vector>Sinclude<string>ttinclude<algorithm>#include<iterator>usingnamespacestd;classIDfriendbooloperator<(constID&,constID&);public:ID(stringname,intscore):name(name),score(score){}voiddisplay()(cout.setf(ios::left);cout?setw(3)<<score?name?endl;}private:stringname;intscore;);//—— //comperationfunctionforsortingbooloperator<(constID&a,constID&b)(returna.score<b.score;)// typedefvector<ID>Vector;//newnameforexistingdatatypeintmain(){Vectorv;Vector::iteratorIter;v.push_back(ID("SmithA",96));v.push_back(IDC'AmstrongB.91));v.push_back(ID("WatsonD.",82));for(Iter=v.begin();Iter!=v.end();Iter++)Iter->display();sort(v.begin(),v.end());//sortalgorithmcout<<endl?"SortedbyScore"<<endl;cout?〃============ニ〃<<endl;for(Iter=v.begin();Iter!=v.end();Iter++)Iter->display();cout?endl?"Reverseoutput"?endl;cout?"==============="<<endl;Vector::reverse_iteratorr=v.rbeginO;while(r!=v.rend())cout?r->display();cout?endl;return0;OUTPUT://96SmithA.//91AmstrongB.//82WatsonD.////SortedbyScore//===============//82WatsonD.//91AmstrongB.//96SmithA.////Reverseoutput//===============//96SmithA.//91AmstrongB.//82WatsonD.reserveSinclude<iostream>#include<vector>usingnamespacestd;intmain()(vector<int>v(5,0);//5elements,each-value0/* */cout<<"Sizeofv="<<v.size()<<endl;cout?"Capacityv="<<v.capacity()<<endl;cout?"Valueofeachelementis一;for(inti=0;i<v.size();i++)cout?v[i]?"";cout?endl;v[0]=5;v[0]=5;v[l]=8;v.push_back(3);//createsnew(6th)elementofvector,v.push_back(3);v.push_back(7);//automaticallyincreasessizecout?endl; //capacityofvectorvcout?”Sizeofv="?v.size()<<endl;cout?"Capacityv="<<v.capacity()<<endl;cout?"Valueofeachelementis-";for(inti=0;i<v.size();i++)cout?v[i]?"cout?endl?endl;v.reserve(100);//increasecapacityto100cout?”Sizeofvlint="?v.size()?endl;cout?”Capacityvl_int="?v.capacity()?endl;intsize=sizeof(v);//howbigisvectoritselfcout?"sizeofv="<<size<<endl;return0;)OUTPUT://Sizeofv=5//Capacityv=5//Valueofeachelementis-00000////Sizeofv=7//Capacityv=10//Valueofeachelementis-5800037////Sizeofv=7//Capacityv=100//sizeofv=12resizeSinclude<iostream>Sinclude<vector>#include<algorithm>Sinclude<iterator>usingnamespacestd;intmain()(vector<int>v(5);for(inti二〇;i<5;i++)v[i]=i*2;copy(v.begin(),v.end(),ostream_iterator<int>(cout,""));cout?endl;v.resize(7,100);copy(v.begin(),v.end(),ostream_iterator<int>(cout,""));cout?endl;v.resize(4);copy(v.begin(),v.end(),ostream_iterator<int>(cout,/Z〃));cout?endl;return0;)OUTPUT://02468//02468100100//0246size#include<iostream>ttinclude<vector>#include<algorithm>ttinclude<iterator>usingnamespacestd;template<classT>classPrint|public:voidoperator()(T&t)(cout?t?""?ヽ));〃==============intmain()(vector<char>v(5);Print<char>print;cout?”Sizeofv="?v.sizeO<<endl;fill(v.begin(),v.end(),'*');for_each(v.begin(),v.end(),print);cout?endl;for(inti=0;i<v.size();i++)cout?v[i]くく"”;cout?endl;for(inti=0;i<5;i++)cout?”Sizeofv=";for_each(v.begin(),v.end(),print);cout?endl;v.popback();}return0;)OUTPUT://Sizeofv=5〃*****〃*****//Sizeofv=*****//Sizeofv=****//Sizeofv二***//Sizeofv二**//Sizeofv二?swap#include<iostream>Sinclude<vector>ttinclude<algorithm>usingnamespacestd;template<classT>classPrint(public:voidoperator()(T&t)(cout?t<<"));//=============intmain()(intary[!二{1,2,3,4,5,6,7,8,9,10};Printprint;vector<int>vl(ary,ary+7);vector<int>v2(ary+7,ary+10);coutくくVectorvl:for_each(vl.begin(),vl.end(),print);cout?endl;cout?”Sizeofvl="?vl.size()?endl?endl;cout?"Vectorv2:";for_each(v2.begin(),v2.end(),print);cout?endl;cout?"Sizeofv2="<<v2.size()?endl?endl;vl.swap(v2);cout?"Afterswapping:<<endl;cout?"Vectorvl:for_each(vl.begin(),vl.end(),print);cout?endl;cout<<"Sizeofvl="<<vl.size()?endl?endl;cout?"Vectorv2:for_each(v2.begin(),v2.end(),print);cout?endl;cout?"Sizeofv2="<<v2.size()?endl?endl;return0;)OUTPUT://Vectorvl:1234567//Sizeofvl=7////Vectorv2:8910//Sizeofv2=3////Afterswapping://Vectorvl:8910//Sizeofvl=3////Vectorv2:1234567//Sizeofv2=7Dequeconstructors#include<iostream>ttinclude<deque>#include<string>ttinclude<algorithm>usingnamespacestd;intmain()(stringstrロ={"Alex","John","Robert"};//emptydequeobjectdeque<int>dl;//createsdequewith10emptyelementsdeque<int>d2(10);//createsdequewith10elements,//andassignvalue0foreachdeque<int>d3(10,0);//createsdequeandassigns//valuesfromstringarraydeque<string>d4(str+0,str+3);deque<string>::iteratorsit=d4.begin();while(sit!=d4.end())cout?*slt++くく””;cout?endl;//copyconstructordeque<string>d5(d4);for(inti=0;i<3;i++)cout?d5[i]<<"";cout?endl;return0;)OUTPUT://AlexJohnRobert//AlexJohnRobertassignSinclude<iostream>Sincludeくdeque)ttinclude<algorithm>#include<iterator>usingnamespacestd;intmain()(intaryロ={1,2,3,4,5);dequeくint>d;//assigntothe〃d〃thecontainsof"ary〃d.assign(ary,ary+5);copy(d.begin(),d.end(),ostream_iterator<int>(cout,z,"));cout?endl;//replacedfor3copiesof100d.assign(3,100);copy(d.begin(),d.end(),ostream_iterator(cout,,Z〃));cout?endl;return0;)OUTPUT://12345//100100100at#include<iostream>ttinclude<deque>usingnamespacestd;intmain()(deque<int>d(3,0);d[0]=100;d.at(1)=200;for(inti=0;i<3;i++)cout?d.at(i)<<cout?endl;return0;)OUTPUT://1002000back#include<iostream>ttinclude<deque>Sinclude<string>#include<iterator>usingnamespacestd;template<classT,classD>classMember(public:Member(Tt,Dd):name(t),sal(d){}voidprint();private:Tname;Dsal;};template<classT,classD>voidMember::print()(cout<<name?""<<sal<<endl;)〃====================intmain()(typedefMember<string,double>M;deque<M>d;d.push_back(M("Robert”,60000));d.push_back(M("Linda",75000));deque<M>::iteratorIt=d.begin();cout?"Entiredeque:"くくendl;while(It!=d.end())(It++)ー>print();cout?endl;cout?"Returnfromback()"?endl;d.back().print();return0;OUTPUT://Entiredeque://Robert60000//Linda75000////Returnfromback()//Linda75000beginSinclude<iostream>#include<deque>#include<iterator>ttinclude<numeric>usingnamespacestd;intmain(){deque<int>d(5);iota(d.begin(),d.end(),1);deque<int>::iteratorIt=d.begin();while(It!=d.end())cout?*It++くく””;cout?endl;//thirdelementofthedequeIt=d.begin()+2;cout?*It<<endl;return0;}OUTPUT://12345//3clear#include<iostream>ttinclude<deque>ttinclude<algorithm>usingnamespacestd;template<classT>classPrint(public:voidoperator()(T&t)(cout?t<<“));〃==============intmain()(deque<int>d(10);Print<int>print;fill(d.beginO,d.end(),5);cout?”Dequed:for_each(d.begin(),d.end(),print);cout?endl;cout?”Sizeofd="?d.size()?endl;cout?"d.clear"<<endl;d.clear();cout?"Dequed:";for_each(d.begin(),d.end(),print);cout?endl;cout?”Sizeofd="くくd.size()?endl;cout?"Dequedis";d.empty()?cout?"":cout<<"notcout?"empty"<<endl;return0;)//Dequed:5555555555//Sizeofd=10//d.clear//Dequed://Sizeofd=0//Dequedisemptyempty#include<iostream>Sinclude<deque>usingnamespacestd;intmain()(deque<int>d;cout?”Dequeisd.empty()?coutくく:cout?"notcout?”empty”<<endl;d.push_back(100);cout?"Dequeis";d.empty()?cout<<"":coutくくnotcout?"empty"?endl;return0;}//Dequeisempty//DequeisnotemptyendSinclude<iostream>ttinclude<deque>^include<iterator>ttinclude<numeric>usingnamespacestd;intmain()(deque<int>d(5);iota(d.begin(),d.end(),1);deque<int>::iteratorIt=d.begin();while(It!=d.end())cout?*It++くく””;cout?endl;//lastelementofthedequeIt=d.end()-1;cout?*It?endl;return0;}OUTPUT://12345//5eraseSinclude<iostream>#include<deque>ttinclude<iterator>#include<algorithm>usingnamespacestd;intmain()(deque<int>d(10);deque<int>::iteratorIt;for(inti=0;i<10;i++)d[i]=i+1;copy(d.begin(),d.end(),ostream_iterator<int>(cout,〃));cout<<endl;It=d.begin()+2;//removethirdelementd.erase(It);copy(d.begin(),d.end(),ostream_iterator<int>(cout,"));cout?endl;It=d.begin();//remove2elementsfrombeginningfodd.erase(It,It+2);copy(d.begin(),d.end(),ostream_iterator<int>(cout,zz"));cout<<endl;return0;)OUTPUT://12345678910//1245678910//45678910frontttinclude<iostream>#include<deque>ttinclude<string>ttinclude<iterator>usingnamespacestd;template<classT,classD>classMemberpublic:Member(Tt,Dd):name(t),sal(d){}voidprint();private:Tname;Dsal;);template<classT,classD>voidMember::print()(cout<<name?""<<sal<<endl;)〃====================intmain(){typedefMember<string,double>M;deque<M>d;d.push_back(M(〃Linda〃,75000));d.push_back(M("Robert”,60000));deque<M>::iteratorIt=d.begin();cout?"Entiredeque:"?endl;while(It!=d.end())(It++)ー>print();cout?endl;cout?"Returnfromfront?!?lt;<endl;d.front().print();return0;)OUTPUT://Entiredeque://Linda75000//Robert60000////Returnfromfront()//Linda75000insertSinclude<iostream>Sinclude<deque>#include<iterator>ttinclude<algorithm>usingnamespacestd;template<classT>classPrint(public:voidoperator()(T&t){cout?t?”“、)};//=============================intmain()(intary[5];fill(ary,ary+5,1);deque<int>d;deque<int>::iteratorIt;Print<int>print;copy(ary,ary+5,back_inserter(d));cout?”dequed :〃;for_each(d.begin(),d.end(),print);cout?endl;It=d.begin();//insertvalue〃5“attheposition〃It〃coutくくd.insert(It,5) :d.insert(It,5);for_each(d.begin(),d.end(),print);cout?endl;//insertrangeary+2-ary+5attheposition"ItIt=d.begin()+5;cout?"d.insert(It,ary+2,ary+5:";d.insert(It,ary+2,ary+5);for_each(d.begin(),d.end(),print);cout?endl;//insert2valueof20'attheposition"It"It=d.end()-2;cout?"d.insert(It,2,20) :d.insert(It,2,20);for_each(d.begin(),d.end(),print);cout?endl;return0;)OUTPUT://dequed :11111//d.insert(It,5) :511111//d.insert(It,ary+2,ary+5:511111111//d.insert(It,2,20) :5111111202011max_size#include<iostream>Sinclude<deque>usingnamespacestd;intmain()(deque<int>d(10);cout?”Sizeofd?d.size()?endl;cout?"Max_sizeofd="?d.max_size()?endl;return0;)OUTPUT://Sizeofd=10//Max_sizeofd=1073741823pop_backttinclude<iostream>#include<deque>Sinclude<algorithm>usingnamespacestd;template<classT>classPrintpublic:voidoperator()(T&t)cout?t<<〃";));〃================intmain()|deque<int>d;Print<int>print;for(inti=0;i<5;i++)d.push_back(i+l);while(!d.empty()){for_each(d.begin(),d.end(),print);cout?endl;d.pop_back();}return0;)OUTPUT:TOC\o"1-5"\h\z// 1 2 3 45// 1 2 3 4// 1 2 3// 1 2//1pop_frontSinclude<iostream>Winclude<deque>Sinclude<algorithm>usingnamespacestd;template<classT>classPrint{public:voidoperator()(T&t){coutくくtくく”";});//=============================intmain()deque<int>d;Print<int>print;for(inti二〇;i<5;i++)d.push_back(i+1);while(!d.empty()){for_each(d.begin(),d.end(),print);cout?endl;d.pop_front();}return0;)OUTPUT://12345//2345//345//45//5push_back#include<iostream>ttinclude<deque>#include<string>Sinclude<iterator>usingnamespacestd;template<classT>className(public:Name(Tt):name(t){}voidprint()(cout?name?〃〃}private:Tname;);〃=============================intmain()typedefName<string>N;typedefdeque<N>D;Dd;Nnl("Robert");Nn2("Alex");d.push_back(nl);d.push_back(r)2);//unnamedobjectofthetypeNamed.push_back(N("Linda"));D::iteratorIt=d.begin();while(It!=d.end())(It++)ー>print();cout<<endl;return0;)OUTPUT://RobertAlexLindapush_frontttinclude<iostream>^include<deque>ttinclude<string>ttinclude<iterator>usingnamespacestd;template<classT>className{public:Name(Tt):name(t){}voidprint(){cout?name?"")private:Tname;};〃=============================intmain()typedefName<string>N;typedefdeque<N>D;Dd;Nnl("Robert");Nn2("Alex");d.push_front(nl);d.push__front(n2);//unnamedobjectofthetypeNamed.push_front(N("Linda"));D::iteratorIt=d.begin();while(It!=d.end())(It++)ー>print();cout?endl;return0;)OUTPUT://LindaAlexRobertrbeginandrend#include<iostream>Sinclude<iomanip>ttinclude<deque>#include<string>ttinclude<algorithm>ttinclude<iterator>usingnamespacestd;classID(friendbooloperator<(constID&,constID&);public:ID(stringname,intscore):name(name),score(score){}voiddisplay(){cout.setf(ios::left);cout?setw(3)<<score<<name?endl;)private:stringname;intscore;};// //comperationfunctionforsortingbooloperatorく(constID&a,constID&b)returna.score<b.score;// typedefdeque<ID>Deque;//newnameforexistingdatatypeintmain()(Dequed;Deque::iteratorIter;d.push_back(ID(z,SmithA”,96));d.push_back(IDC'AmstrongB.",91));d.push_back(IDC'WatsonD.”,82));for(Iter=d.begin();Iter!=d.end();Iter++)Iter->display();sort(d.begin(),d.end());//sortalgorithmcout<<endlくくSortedbyScore”<<endl;cout?〃=============ニニ〃<<endl;for(Iter=d.begin();Iter!=d.end();Iter++)Iter->display();cout?endl?"Reverseoutput"?endl;cout?〃二二ニニニニニニニニニニニニ二“<<endl;Deque::reverse_iteratorr=d.rbeginO;while(r!=d.rend())cout?redisplay();cout?endl;return0;)OUTPUT://96SmithA.//91AmstrongB.//82WatsonD.////SortedbyScore//===============//82WatsonD.//91AmstrongB.//96SmithA.////Reverseoutput//===============//96SmithA.//91AmstrongB.//82WatsonD.resizeSinclude<iostream>ttinclude<deque>#include<algorithm>#include<iterator>usingnamespacestd;intmain()(deque<int>d(5);for(inti=0;i<5;i++)d[i]=i*2;copy(d.begin(),d.end(),ostream_iterator<int>(cout,zZ〃));cout?endl;d.resize(7,100);copy(d.begin(),d.end(),ostream_iterator<int>(cout,z/〃));cout?endl;d.resize(4);copy(d.begin(),d.end(),ostream_iterator<int>(cout,zz"));cout?endl;return0;)OUTPUT://02468//02468100100//0246size#include<iostream>ttinclude<deque>ttinclude<algorithm>#include<iterator>usingnamespacestd;template<classT>classPrint(public:voidoperator()(T&t){cout?t?”"));〃=============================intmain(){deque<char>d(5);Print<char>print;cout?”Sizeofd="?d.size()<<endl;fill(d.beginO,d.end(),'*');for_each(d.begin(),d.end(),print);cout?endl;for(inti=0;i<d.size();i++)cout?d[i]?"'?ヽcout?endl;for(inti=0;i<5;i++)(cout?"Sizeofd=";for_each(d.begin(),d.end(),print);cout?endl;d.popback();)return0;)OUTPUT://Sizeofd=5〃*****〃*****//Sizeofd二*****//Sizeofd=****//Sizeofd二***//Sizeofd二**//Sizeofd二?swapttinclude<iostream>#include<deque>Sinclude<algorithm>usingnamespacestd;template<classT>classPrint(public:voidoperator()(T&t){coutくくtくく”";));〃=============================intmain()(intary[]={1,2,3,4,5,6,7,8,9,10};Printprint;deque<int>dl(ary,ary+7);deque<int>d2(ary+7,ary+10);cout?”Dequedl:";for_each(dl.begin(),dl.end(),print);cout?endl;cout?"Sizeofdl="<<dl.size()?endl?endl;cout<<"Dequed2:";for_each(d2.begin(),d2.end(),print);cout<<endl;cout?"Sizeofd2="?d2.size()?endl?endl;dl.swap(d2);cout?"Afterswapping:"<<endl;cout?"Dequedl:for_each(dl.begin(),dl.end(),print);cout?endl;cout?"Sizeofdl="<<dl.size()?endl?endl;cout?"Dequed2:〃;for_each(d2.begin(),d2.end(),print);cout?endl;?d2.size()cout<<"Sizeofd2?d2.size()?endl?endl;return0;)OUTPUT://Dequedl:123456//Sizeofdl=7////Dequed2:8910//Sizeofd2=3////Afterswapping://Dequedl:8910//Sizeofdl=3////Dequed2:123456//Sizeofd2=7listassign//assignasequencetothelistSinclude<iostream>Sinclude<list>ttinclude<algorithm>#include<iterator>usingnamespacestd;intary[]={l,2,3,4,5);list<int>1;//assignto1thecontainsofary1.assign(ary,ary+5);copy(1.begin(),1.end(),ostreamiteratorくint>(cout,〃〃));cout?endl;//replace1for3copiesof1001.assign(3,100);copy(1.begin(),1.end(),ostream_iterator<int>(cout,〃));cout<<endl;return0;)OUTPUT://12345//100100100back//returnsthelastelement#include<iostream>#include<list>Winclude<algorithm>Sinclude<string>Sinclude<iterator>usingnamespacestd;template<classT,classD>classMember(public:Member(Tt,Dd):name(t),sal(d){)voidprint();private:Tname;};template<classT,classD>voidMember<T,D>::print()(cout?name?”"?sal<<endl;)// intmain()(typedefMember<string,double>M;list<M>1;1.push_back(M(zzRobert,z,60000));l.push_back(M(zzLinda,z,75000));list<M>::iteratorIt=1.begin();cout?,Entirelist:"?endl;while(It!=1.end())(It++)ー〉print();cout?endl;cout?"Returnfromback()"<<endl;1.back().print();return0;)OUTPUT://Entirelist://Robert60000//Linda75000////Returnfromback()//Linda75000begin//returnsaniteratortothebeginningttinclude<iostream>ttinclude<list>ttinclude<algorithm>#include<iterator>Sinclude<numeric>usingnamespacestd;intmain()(list<int>1(5);iota(l.begin(),1.end(),1);list<int>::iteratorIt=1.begin();while(It!=1.end())cout?*It++くく"〃;cout<<endl;//thirdelementofthelistIt=1.begin()+2;cout<<*It<<endl;return0;)OUTPUT://12345//3clear//removesallelementsSinclude<iostream>Sinclude<list>usingnamespacestd;intmain(){list<int>1(5,10);cout?”Sizeoflist="?1.size()?endl;1.clear();cout?"After1.clear()sizeoflist=?1.size()?endl;return0;OUTPUT://Sizeoflist=5//After1.clear()sizeoflist=0empty//truei

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論