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

    C++STL范例大全教程.docx

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

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

    C++STL范例大全教程.docx

    Sample of STLSTL范例(一)容器部分Vector1Deque20List38Set66Multiset88Map98Multimap113Stack136Queue138Priorit yqueue139VectorconstructorsSinclude <iostream>Sinclude <vector> ttinclude <string> #include <algorithm> using namespace std; int main () (string str="Alex", "John", "Robert"/ empty vector object vector<int> vl;/ creates vector with 10 empty elements vector<int> v2 (10);/ creates vector with 10 elements,/ and assign value 0 for each vector<int> v3(10, 0);/ creates vector and assigns/ values from string array vector<string> v4(str+0, str+3);vector<string>:iterator sit = v4.begin();while ( sit != v4. end() cout « *slt+ ”; cout « endl;/ copy constructorvector<string> v5(v4);for ( int i=0; i<3; i+ )cout « v5i « ""cout « endl;return 0;OUTPUT:/ Alex John Robert/ Alex John Robert assign#include <iostream> ttinclude <vector> ttinclude <algorithm> ttinclude <iterator> using namespace std;int main ()(int ary =1, 2, 3, 4, 5);vector<int> v;/ assign to the 'v the contains of aryv. assign (ary, ary+5);copy (v. begin (), v. end (), ostream_iterator<int>(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 <iostream> ttinclude <vector> using namespace std; int main ()(vector<int> v(3, 0);v0 = 100;v. at (1) = 200;for ( int i=0; i<3; i+ ) cout « v. at (i) << ”cout « endl;return 0;)OUTPUT:/ 100 200 0back ttinclude <iostream>#include <vector>Sinclude <string> Sinclude <iterator> using namespace std; template<class T, class D> class Member public:Member (T t, D d) : name(t), sal (d) void print ();private: T name; D sal; ;template<class T, class D> void Member:print() cout « name « ” " « sal << endl;int main () typedef Member<string, double> M; vector<M> v;v.push_back(M("Robert”, 60000);v.push_back(M("Linda", 75000); vector<M>: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 75000 beginSinclude <iostream>#include <vector> ttinclude <iterator> #include <numeric> using namespace std;int main ()(vector<int> v(5);iota(v. begin (), v. end(), 1);vector<int>: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/ 3capacitySinclude <iostream> Sinclude <vector> using namespace std; int main () vector<int> 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/ Capacity of v = 100clear ttinclude <iostream> Sinclude <vector> Sinclude <algorithm> using namespace std; template <class T> class Print public:void operator () (T& t) (cout « t << “ ;int main ()(vector<int> v(10);Print<int> 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 « "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 emptyemptySinclude <iostream>#include <vector> using namespace std;int main ()(vector<int> 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 <iostream> Sinclude <vector> ttinclude <iterator> #include <numeric> using namespace std; int main ()vector<int> v(5); iota(v. begin (), v. end (),1); vector<int>: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 ttinclude <iostream>#include <vector>ttinclude <iterator>#include <algorithm> using namespace std;int main ()(vector<int> v(10);vector<int>:iterator It;for ( int i=0; i<10; i+ )vi = i+1;copy (v. begin (), v. end(), ostream_iteratorint>(cout, );cout << endl;It = v. begin() +2;/ remove third elementv. erase (It);copy (v. begin (), v. end(), ostream_iteratorint>(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:/ 123456789 10/ 12456789 10/ 4 5 6 7 8 9 10front ttinclude <iostream> Sinclude <vector> ttinclude <string> #include <iterator> using namespace std; template<class T, class D> class Member (public:Member (T t, D d) : name(t), sal (d) void print ();private: T name; D sal; ;template<class T, class D> void Member:print() (cout « name « ” " « sal << endl;)= int main () (typedef Member<string, double> M; vector<M> v;v.push_back(M("Linda”, 75000);v. push_back(M("Robert”, 60000);vector<M>: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 front()/ Linda 75000 insertSinclude <iostream>#include <vector> Sinclude <iterator> #include <algorithm> using namespace std; template <class T> class Print (public:void operator () (T& t) (cout << t « );= int main () (int ary5;fill (ary, ary+5, 1); vector<int> v;vector<int>:iterator It;Print<int> 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. 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, 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 « "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 <iostream>#include <vector> using namespace std; int main ()vector<int> 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 <iostream>Sinclude <vector>Sinclude <algorithm> using namespace std; template <class T> class Print public:void operator () (T& t) (cout « t « "" );= int main () (vector<int> v;Print<int> print;for ( int i=0; i<5; i+ ) v. push_back(i+l);while ( !v. empty () (for_each(v. begin (), v. end(), print); cout « endl;v. pop_back ();) return 0;)OUTPUT:/ 1 2 3 4 5/ 1 2 3 4/ 1 2 3/ 1 2 / 1push_backSinclude <iostream> ttinclude <vector> #include <string> #include <iterator> using namespace std; template <class T> class Namepublic:Name (T t) : name (t) void print() (cout « name « ;) private:T name;);= int main ()(typedef Name<string> N;typedef vector<N> 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 Alex Lindarbegin and rend#include <iostream> ttinclude <iomanip> #include <vector> Sinclude <string> ttinclude <algorithm> #include <iterator> 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; int score;);/ comperation function for sortingbool operator < ( const ID& a, const ID& b ) (return a.score < b.score;)/typedef vector<ID> Vector; / new name for existing datatypeint main () Vector v;Vector:iterator Iter;v.push_back(ID("Smith A",96);v. push_back (ID C'Amstrong B. 91);v. push_back (ID ("Watson D. ", 82);for ( Iter = v. begin(); Iter != v. end() ; Iter+ )Iter->display();sort (v. begin(), v. end() ; / sort algorithmcout << endl « "Sorted by Score" << endl;cout « = << endl;for ( Iter = v. begin() ; Iter != v. end() ; Iter+ ) Iter->display();cout « endl « "Reverse output" « endl;cout « "=" << endl;Vector:reverse_iterator r = v. rbeginO;while ( r != v. rend()cout « r->display();cout « endl;return 0;OUTPUT:/ 96 Smith A./ 91 Amstrong B./ 82 Watson D./ Sorted by Score/ =/ 82 Watson D./ 91 Amstrong B./ 96 Smith A./ Reverse output/ =/ 96 Smith A./ 91 Amstrong B./ 82 Watson D.reserveSinclude <iostream>#include <vector> using namespace std;int main ()(vector<int> 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; 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 element 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/ 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 = 12 resizeSinclude <iostream>Sinclude <vector>#include <algorithm>Sinclude <iterator> using namespace std;int main () (vector<int> v(5);for ( int i二; i<5; i+ ) vi = 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;return 0;)OUTPUT:/ 0 2 4 6 8/ 0 2 4 6 8 100 100/ 0 2 4 6size#include <iostream> ttinclude <vector> #include <algorithm> ttinclude <iterator> using namespace std; template <class T> class Print |public:void operator () (T& t) (cout « t « "" );= int main () (vector<char> v(5); Print<char> 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; i<5; i+ )cout « ”Size of v =";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 <iostream>Sinclude <vector> ttinclude <algorithm> using namespace std;template <class T> class Print (public:void operator () (T& t) (cout « t << " );/= int main ()(int ary !二1, 2, 3, 4, 5, 6, 7, 8, 9, 10;Print print;vector<int> vl(ary, ary+7);vector<int> 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 « 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 : 1234567/ Size of v2 = 7Dequeconstructors#include <iostream>ttinclude <deque>#include <string>ttinclude <algorithm> using namespace std; int main ()(string str="Alex", "John", "Robert"/ empty deque object deque<int> dl;/ creates deque with 10 empty elements deque<int> d2 (10);/ creates deque with 10 elements,/ and assign value 0 for each deque<int> d3(10,0);/ creates deque and assigns/ values from string array deque<string> d4(str+0,str+3);deque<string>:iterator sit = d4. begin();while ( sit != d4. end() cout « *slt+ ”; cout « endl;/ copy constructordeque<string> d5(d4);for ( int i=0; i<3; i+ )cout « d5i << ""cout « endl;return 0;)OUTPUT:/ Alex John Robert/ Alex John RobertassignSinclude <iostream>Sinclude deque) ttinclude <algorithm> #include <iterator> using namespace std;int main ()(int ary = 1, 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<int>(cout, 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;)OU

    注意事项

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

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




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

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

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

    收起
    展开