C++STL范例大全教程.docx
《C++STL范例大全教程.docx》由会员分享,可在线阅读,更多相关《C++STL范例大全教程.docx(120页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Sample of STLSTL范例(一)容器部分Vector1Deque20List38Set66Multiset88Map98Multimap113Stack136Queue138Priorit yqueue139VectorconstructorsSinclude Sinclude ttinclude #include using namespace std; int main () (string str=Alex, John, Robert;/ empty vector object vector vl;/ creates vector with 10 empty elements
2、vector 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
3、=0; i3; 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_iterato
4、r(cout, z, zz);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:/ 1 2 3 4 5/ 100 100 100atinclude ttinclude using namespace std; int main ()(vector v(3, 0);v0 = 100;v. at (1) = 200;for ( int i=0; i3; i+ )
5、cout v. at (i) ”cout endl;return 0;)OUTPUT:/ 100 200 0back ttinclude #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
6、; vector v;v.push_back(M(Robert”, 60000);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
7、()/ Linda 75000 beginSinclude #include ttinclude #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:/ 1 2 3 4 5/
8、 3capacitySinclude Sinclude 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 = v. size() endl;cout Capacity of v = v. capacity () endl;return 0;OUTPUT:/ Size of v = 10/ Ca
9、pacity of v = 10/ After resizing:/ Size of v = 100/ Capacity 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
10、(), print);cout endl;cout ”Size of v = v. size() endl;cout 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
11、. clear/ Vector v :/ Size of v = 0/ Vector v is emptyemptySinclude #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 no
12、t emptyendSinclude Sinclude ttinclude #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;/ last element of the vectorIt = v. end()-1;cout *It endl;return 0;)OUTPUT:/ 1 2 3 4 5/ 5erase ttincl
13、ude #include ttinclude #include using namespace std;int main ()(vector v(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
14、:/ 123456789 10/ 12456789 10/ 4 5 6 7 8 9 10front ttinclude Sinclude ttinclude #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;
15、vector v;v.push_back(M(Linda”, 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() endl;v. front (). print ();return 0;)OUTPUT:/ Entire vector:/ Linda 75000/ Robert 60000/ Return from
16、front()/ Linda 75000 insertSinclude #include Sinclude #include using namespace std; template 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. be
17、gin (), v. end(), print);cout endl;It = v. begin();/ insert value 5 at the position It cout v. insert (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, ar
18、y+2, ary+5);for_each (v. begin (), v. end (), print);cout endl;/ insert 2 value of 20 at the position 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.
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- STL 范例 大全 教程
限制150内