C++_STL范例大全_教程.docx
《C++_STL范例大全_教程.docx》由会员分享,可在线阅读,更多相关《C++_STL范例大全_教程.docx(124页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Sample of STLSTL范例(一)容器部分Vector1Deque20L i st38Set66Mult i set88Map98Stack136Queue138Priori ty_queue139VectorconstructorsSinclude #include Sinclude Sinclude using namespace std; int main () |string str=Alex, John, Robert;/ empty vector object vector vl;/ creates vector with 10 empty elements vector
2、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 constructor vector v5(v4); for ( int i=0; i3;
3、 i+ ) cout v5i ;cout endl; return 0;)OUTPUT:/ Alex John Robert/ Alex John Robert assign#include Sinclude #include #include using namespace std;int main ()(int ary=l, 2, 3, 4, 5);vector v;/ assign to the v the contains of aryv. assign (ary, ary+5);copy (v. beginO, v. end(), ostream_iterator(cout,);co
4、ut endl;/ replace v for 3 copies of 100v. assign (3, 100);copy (v. beginO, v. end(), ostream_iterator(cout,);cout endl;return 0;OUTPUT:/ 1 2 3 4 5/ 100 100 100at#include #include 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;r
5、eturn 0;)OUTPUT:/ 100 200 0back#include #include #include #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; vector v;v. push_back(M
6、(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()/ Linda 7500
7、0 begin#include #include #include 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/ 3capacity#includ
8、e Winclude 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. sizeO endl;cout Capacity of v = v. capacity () endl;return 0;OUTPUT:/ Size of v = 10/ Capacity of v = 10/ Af
9、ter resizing:/ Size of v = 100/ Capacity of v = 100 clearWinclude Winclude ttinclude using namespace std; template class Print (public:void operator () (T& t) (cout t ” );/ /一二二二 int main () (vector v(10);Print print;fill (v. beginO, v. end(), 5);cout Vector v :;for_each(v. begin(), v. end(), print)
10、; cout endl;cout Size of v = v. size() endl;cout v.clear endl;v. clear ();cout Vector v :for_each(v. beginO, v. end(), print); cout endl;cout Size of v = v. size() endl;cout Vector v is ;v. empty() ? cout : cout notcout emptyz, endl;return 0;)/ Vector v : 5555555555/ Size of v = 10/ v. clear/ Vector
11、 v :/ Size of v = 0/ Vector v is empty emptySinclude #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 emptyendSi
12、nclude Sinclude Sinclude #include using namespace std; int main ()vector v(5);iota(v. beginO, 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#include #include #includ
13、e Sinclude using namespace std;int main () | vector v(10); vector:iterator It; for ( int i=0; i(cout, );cout endl;It = v. begin()+2;/ remove third elementv. erase (It);copy (v. begin (), v. end (), ostream_iteratorint(cout, );cout endl;It = v. beginO ;/ remove 2 elements from beginning fo vv. erase
14、(It, It+2);copy (v. beginO, v. end(), ostream_iteratorint(cout,);cout endl;return 0;OUTPUT:/ 123456789 10/ 12456789 10/ 4 5 6 7 8 9 10frontSinclude #include Winclude Winclude using namespace std; template class Member (public:Member (T t, D d) : name(t), sal (d) void print ();private: T name; D sal;
15、 );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. begin(); cout Entire vector: print ();cout endl;cout Return from front() endl;v. front (). print ();return 0;)OUTPUT
16、:/ Entire vector:/ Linda 75000/ Robert 60000/ Return from front()/ Linda 75000 insertWinclude #include Sinclude Sinclude 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 (
17、ary, ary+5, back_inserter(v);cout ”vector v:for_each (v. begin (), v. end (), print); cout endl;It = v. beginO ;/ insert value 5 at the position It cout v. insert (It, 5):;v. insert (It, 5);for_each(v. beginO, v. end(), print);cout endl;/ insert range ary+2 - ary+5 at the position It = v. begin()+5;
18、cout 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 It It = v. end()-2;cout z,v. insert (It, 2, 20):;v. insert (It, 2, 20);for_each (v. begin (), v. end(), print);cout endl;return 0;)OUTPUT:/ vector v
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- _STL 范例 大全 教程
限制150内