c_stl范例大全
《c_stl范例大全》由会员分享,可在线阅读,更多相关《c_stl范例大全(120页珍藏版)》请在装配图网上搜索。
1、Sample of STLSTL范例(一)容器部分Vector1Deque20List38Set66Multiset88Map98Multimap113Stack136Queue138139PriorityqueueVectorconstructorsSinclude Sinclude ttinclude #include using namespace std; int main ()(string str=Alex,John,Robert;/ empty vector object vector vl;/ creates vector with 10 empty elements vect
2、or v2(10);/ creates vector with 10 elements,/ and assign value 0 for each vector v3(10,0);/ creates vector and assigns/ values from string array vector 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;
3、 i+)cout v5i;cout endl;return 0;OUTPUT:/ Alex John Robert/ Alex John Robert assign#include ttinclude ttinclude ttinclude using namespace std;int main ()(int ary =1,2,3,4,5);vector v;/ assign to the v the contains of aryv. assign (ary, ary+5);copy (v. begin (), v. end (), ostream_iterator(cout, z, zz
4、);cout endl;/ replace v for 3 copies of 100v. assign (3,100);copy (v. begin (), v. end(), ostream_iterator (cout,zz );cout endl;return 0;)OUTPUT:/12345/100100100atinclude ttinclude using namespace std; int main ()(vector v(3,0);v0=100;v. at (1)=200;for ( int i=0; i3; i+) cout v. at (i)”cout endl;ret
5、urn 0;)OUTPUT:/1002000backttinclude #include Sinclude Sinclude using namespace std; template class Member public:Member (T t, D d): name(t), sal (d) void print ();private: T name; D sal;template void Member:print()cout name ” sal endl;int main ()typedef Member M; vector v;v.push_back(M(Robert”,60000
6、);v.push_back(M(Linda,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;)OUTPUT:/ Entire vector:/ Robert 60000/ Linda 75000/ Return from back()/ Linda 75000beginSinclude #include tt
7、include #include 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:/12345/3capacitySinclude Sinclude using namespace std;
8、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 = v. size() endl;cout Capacity of v = v. capacity () endl;return 0;OUTPUT:/ Size of v =10/ Capacity of v =10/ After resizing:/ Size of v =100/ C
9、apacity of v =100clear ttinclude Sinclude Sinclude using namespace std; template class Print public:void operator ()(T& t)(cout t “;int main ()(vector v(10);Print print;fill (v. begin(), v. end(),5);cout ,Vector v :for_each(v. begin(), v. end(), print);cout endl;cout ”Size of v = v. size() endl;cout
10、 v. clear endl;v. clear ();cout Vector v :;for_each (v. begin (), v. end (), print);cout endl;cout Size of v = v. sizeO endl;cout Vector v is ;v. empty ()? cout : cout notcout ”empty” endl;return 0;)/ Vector v :5555555555/ Size of v =10/ v. clear/ Vector v :/ Size of v =0/ Vector v is emptyemptySinc
11、lude #include using namespace std;int main ()(vector v;cout Vector is ;v. empty ()? cout : cout not cout empty endl;v. push_back(100);cout Vector is ;v. empty ()? cout : cout not cout empty endl;return 0;)/ Vector is empty/ Vector is not emptyendSinclude Sinclude ttinclude #include using namespace s
12、td; int main ()vector v(5); iota(v. begin (), v. end (),1); vector:iterator It = v.begin();while ( It != v. end()cout *It+”工cout endl;/ last element of the vectorIt = v. end()-1;cout *It endl;return 0;)OUTPUT:/12345/5erasettinclude #include ttinclude #include using namespace std;int main ()(vector v
13、(10);vector:iterator It;for ( int i=0; i(cout,);cout (cout,);cout endl;It = v. begin();/ remove 2 elements from beginning fo vv. erase (It, It+2);copy (v. begin(), v. end(), ostream_iteratorint(cout,);cout endl;return 0;OUTPUT:/12345678910/1245678910/45678910frontttinclude Sinclude ttinclude #includ
14、e using namespace std; template class Member (public:Member (T t, D d): name(t), sal (d) void print ();private: T name; D sal;template void Member:print()(cout name ” sal endl;)= int main ()(typedef Member M; vector v;v.push_back(M(Linda”,75000);v. push_back(M(Robert”,60000);vector:iterator It = v.b
15、egin(); cout ”Entire vector:“ endl;while ( It != v. end()(It+)print ();cout endl;cout Return from front() endl;v. front (). print ();return 0;)OUTPUT:/ Entire vector:/ Linda 75000/ Robert 60000/ Return from front()/ Linda 75000 insertSinclude #include Sinclude #include using namespace std; template
16、class Print (public:void operator ()(T& t)(cout t );= int main ()(int ary5;fill (ary, ary+5,1); vector v;vector:iterator It;Print print; copy (ary, ary+5, back_inserter(v);cout ”vector v:;for_each (v. begin (), v. end(), print);cout endl;It = v. begin();/ insert value 5 at the position It cout v. in
17、sert (It,5):;v. insert (It,5);for_each (v. begin (), v. end (), print);cout endl;/ insert range ary+2- ary+5 at the position 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;/ insert 2 value of 20 at the position
18、 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;return 0;)OUTPUT:/ vector v/ v. insert (It, 5)/ v. insert(It, ary+2, ary+5/ v. insert (It, 2, 20)111115 111115 111111115 1 1 1 1 1 1 20 20 1 1max_size ttinclude #include using namespa
19、ce std; int main ()vector v(10);cout Size of v v. size() endl;cout Max_size of v = v. max_size () endl;return 0;OUTPUT:/ Size of v =10/ Max_size of v =1073741823pop_back#include Sinclude Sinclude using namespace std; template class Print public:void operator ()(T& t)(cout t );= int main ()(vector v;
20、Print print;for ( int i=0; i5; i+) v. push_back(i+l);while (!v. empty ()(for_each(v. begin (), v. end(), print); cout endl;v. pop_back ();) return 0;)OUTPUT:/12345/1234/123/12/1push_backSinclude ttinclude #include #include using namespace std; template class Namepublic:Name (T t): name (t) void prin
21、t()(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 Name v. push_back(N(Linda);V: iterator It = v. begin();while ( It != v. end()(It+)print ();cout endl;return 0;)OUTPUT:/ Robert Ale
22、x Lindarbegin and rend#include ttinclude #include Sinclude ttinclude #include using namespace std;class IDfriend bool operator ( const ID&, const ID&); public:ID(string name, int score): name(name), score(score) void display ()(cout. setf(ios:left);cout setw(3) score name endl;private:string name; i
23、nt score;);/ comperation function for sortingbool operator ( const ID& a, const ID& b )(return a.score b.score;)/typedef vector Vector;/ new name for existing datatypeint main ()Vector v;Vector:iterator Iter;v.push_back(ID(Smith A,96);v. push_back (ID CAmstrong B.91);v. push_back (ID (Watson D.,82);
24、for ( Iter = v. begin(); Iter != v. end(); Iter+)Iter-display();sort (v. begin(), v. end();/ sort algorithmcout endl Sorted by Score endl;cout =display();cout endl Reverse output endl;cout =display();cout endl;return 0;OUTPUT:/96 Smith A./91 Amstrong B./82 Watson D./ Sorted by Score/=/82 Watson D./9
25、1 Amstrong B./96 Smith A./ Reverse output/=/96 Smith A./91 Amstrong B./82 Watson D.reserveSinclude #include using namespace std;int main ()(vector v(5,0);/5 elements, each - value 0/*/cout Size of v = v. size() endl;cout Capacity v = v.capacity() endl;cout Value of each element is 一;for ( int i =0;
26、i v. size(); i+) cout vi;cout endl;v0 = 5;/ new value for first elementvl=8;v.push_back(3);/ creates new (6th) element of vector,v. push_back(7);/ automatically increases size cout endl;/ capacity of vector vcout ”Size of v = v. size() endl; cout Capacity v = v.capacity() endl; cout Value of each el
27、ement is -;for ( int i =0; i v. size(); i+)cout vi cout endl endl;v. reserve(100);/ increase capacity to 100cout ”Size of vl int = v. size() endl;cout ”Capacity vl_int = v.capacity() endl; int size = sizeof(v);/ how big is vector itself cout sizeof v = size endl;return 0;)OUTPUT:/ Size of v =5/ Capa
28、city v =5/ Value of each element is -00000/ Size of v =7/ Capacity v =10/ Value of each element is -5800037/ Size of v =7/ Capacity v =100/ sizeof v =12 resizeSinclude Sinclude #include Sinclude using namespace std;int main ()(vector v(5);for ( int i二; i5; i+) vi= i*2;copy (v. begin (), v. end (), o
29、stream_iterator(cout,);cout endl;v. resize (7,100);copy (v. begin(), v. end(), ostream_iterator(cout,);cout endl;v. resize (4);copy (v. begin (), v. end(),ostream_iterator(cout,/Z ); cout endl;return 0;)OUTPUT:/02468/02468100100/0246size#include ttinclude #include ttinclude using namespace std; temp
30、late class Print |public:void operator ()(T& t)(cout t );= int main ()(vector v(5); Print print; cout ”Size of v = v. sizeO endl; fill (v. begin(), v. end(),*);for_each (v. begin (), v. end(), print); cout endl;for ( int i=0; i v. size(); i+) cout vi”;cout endl;for ( int i=0; i5; i+)cout ”Size of v
31、=;for_each (v. begin (), v. end (), print);cout endl;v. pop back ();return 0;)OUTPUT:/ Size of v =5*/ Size of v=*/ Size of v =*/ Size of v 二*/ Size of v 二*/ Size of v 二swap#include Sinclude ttinclude using namespace std;template class Print (public:void operator ()(T& t)(cout t );/= int main ()(int
32、ary !二1,2,3,4,5,6,7,8,9,10;Print print;vector vl(ary, ary+7);vector v2(ary+7, ary+10);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 end
33、l;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 :1234567/ Size of
34、vl =7/ Vector v2:8910/ Size of v2=3/ After swapping:/ Vector vl :8910/ Size of vl =3/ Vector v2:1234567/ Size of v2=7Dequeconstructors#include ttinclude #include ttinclude using namespace std; int main ()(string str=Alex,John,Robert;/ empty deque object deque dl;/ creates deque with 10 empty element
35、s deque d2(10);/ creates deque with 10 elements,/ and assign value 0 for each deque d3(10,0);/ creates deque and assigns/ values from string array deque d4(str+0,str+3);deque:iterator sit = d4. begin();while ( sit != d4. end() cout *slt+”; cout endl;/ copy constructordeque d5(d4);for ( int i=0; i3;
36、i+)cout d5i;cout endl;return 0;)OUTPUT:/ Alex John Robert/ Alex John RobertassignSinclude Sinclude deque) ttinclude #include using namespace std;int main ()(int ary=l,2,3,4,5);dequeint d;/ assign to the d the contains of aryd. assign (ary, ary+5);copy (d. begin (), d. end (), ostream_iterator(cout,
37、z,);cout endl;/ replace d for 3 copies of 100d. assign (3,100);copy (d. begin(), d. end(), ostream_iterator (cout,,Z );cout endl;return 0;)OUTPUT:/12345/100100100at#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;retu
38、rn 0;)OUTPUT:/1002000back#include ttinclude Sinclude #include using namespace std; template class Member (public:Member (T t, D d): name(t), sal (d) void print ();private: T name; D sal;template void Member:print()(cout name sal endl;)= int main ()(typedef Member M; deque d;d. push_back(M(Robert”,60
39、000);d. push_back(M(Linda,75000);deque:iterator It = d.begin(); cout Entire deque: endl; while ( It != d. end()(It+)print ();cout endl;cout Return from back() endl;d. back(). print (); return 0;OUTPUT:/Entiredeque:/Robert60000/Linda75000/Returnfrom back ()/Linda75000beginSinclude #include #include t
40、tinclude 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:/12345/3clear#include ttinclude ttinclude using namespace std; tem
41、plate class Print (public:void operator ()(T& t)(cout t “);= int main ()(deque d(10);Print print;fill (d. beginO, d. end(),5);cout ”Deque d :for_each(d. begin(), d. end(), print);cout endl;cout ”Size of d = d. size() endl;cout d.clear endl;d. clear ();cout Deque d :;for_each (d. begin (), d. end (),
42、 print);cout endl;cout ”Size of d = d. size() endl;cout Deque d is ;d. empty()? cout : cout not cout empty endl;return 0;)/ Deque d:5555555555/ Size of d =10/ d. clear/ Deque d :/ Size of d =0/ Deque d is empty empty #include Sinclude using namespace std;int main ()( deque d; cout ”Deque is d. empty
43、 ()? cout : cout not cout ”empty” endl;d. push_back(100);cout Deque is ;d. empty ()? cout : cout not cout empty endl;return 0;/ Deque is empty/ Deque is not emptyendSinclude ttinclude include ttinclude using namespace std;int main ()(deque d(5);iota(d. begin(), d. end(),1);deque:iterator It = d.begi
44、n();while ( It != d. end()cout *It+”; cout endl;/ last element of the dequeIt = d. end ()-1;cout *It endl;return 0;OUTPUT:/12345/5 eraseSinclude #include ttinclude #include using namespace std;int main ()(deque d(10);deque:iterator It;for ( int i=0; i10; i+)di= i+1;copy (d. begin (), d. end(), ostre
45、am_iterator(cout,);cout endl;It = d. begin()+2;/ remove third elementd. erase (It);copy (d. begin(), d. end(), ostream_iterator(cout,);cout endl;It = d. begin();/ remove 2 elements from beginning fo dd. erase (It, It+2);copy (d. begin(), d. end(), ostream_iterator(cout,zz );cout endl;return 0;)OUTPU
46、T:/12345678910/1245678910/45678910front ttinclude #include ttinclude ttinclude using namespace std; templateclass Memberpublic:Member (T t, D d): name(t), sal (d) void print ();private:T name;D sal;);templatevoid Member:print()(cout name sal endl;)=int main ()typedef Member M;deque d;d. push_back(M(
47、Linda,75000);d.push_back(M(Robert”,60000);deque:iterator It = d.begin(); cout Entire deque: endl;while ( It != d. end()(It+)print ();cout endl;cout Return from front。“ endl;d. front (). print ();return 0;)OUTPUT:/ Entire deque:/ Linda 75000/ Robert 60000/ Return from front()/ Linda 75000 insertSincl
48、ude Sinclude #include ttinclude using namespace std;template class Print (public: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
49、= d. begin();/ insert value 5“ at the position Itcout d. insert (It,5):d. insert (It,5);for_each(d. begin(), d. end(), print);cout endl;/ insert range ary+2- ary+5 at the position It It = 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;/ insert 2 value of 20 at the position ItIt = d. end ()-2;cout d. insert (It,2,20):d. insert (It,2,20);for_each(d. begin(), d. end(), print);cout endl;return 0;)OUTPUT:/ deque d:11111/ d. insert (It,5):511111/ d. insert(It, ary+2, ary+5:511111111/ d. insert (It,2,20):511111120
- 温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。