网站大量收购独家精品文档,联系QQ:2885784924

C++模板与STL库PPT.ppt

  1. 1、本文档共92页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
p = find(v.begin(),v.end(),9); if( p == v.end()) cout not found endl; p = find(v.begin()+1,v.end()-2,1); if( p != v.end()) cout * p endl; int * pp = find( array,array+4,20); cout * pp endl; } 输出: 3 not found 3 20 顺序容器(vector deque list )的算法 除前述共同操作外,顺序容器还有以下共同操作: front() :返回容器中第一个元素的引用 back() : 返回容器中最后一个元素的引用 push_back(): 在容器末尾增加新元素 pop_back(): 删除容器末尾的元素 比如,查 list::front 的help,得到的定义是: reference front(); const_reference front() const; list有两个front函数 顺序容器的引用 reference 和 const_reference 是typedef的类型 对于 listdouble , listdouble::reference 实际上就是 double listdouble::const_reference 实际上就是 const double 对于 listint , listint::refrence 实际上就是 int listint::const_refreence 实际上就是 const int vector 支持随机访问迭代器,所有STL算法都能对vector操作。 随机访问时间为常数。在尾部添加速度很快,在中间插入慢。实际上就是动态数组。 例1:#includeiostream #includevector #include algorithmusing namespace std; int main() { int i; int a[5] = {1,2,3,4,5 }; vectorint v(5); cout v.end() - v.begin() endl; for( i = 0;i v.size();i ++ ) v[i] = i; v.at(4) = 100; for( i = 0;i v.size();i ++ ) cout v[i] , ; cout endl; vectorint v2(a,a+5); //构造函数 v2.insert( v2.begin() + 2, 13 ); //在begin()+2位置插入 13 for( i = 0;i v2.size();i ++ ) cout v2[i] , ; return 0; } 输出: 5 0,1,2,3,100, 1,2,13,3,4,5, #include iostream #include vector #include algorithm #include numeric using namespace std; void main() { istream_iteratorint input(cin); int n1,n2; n1 = *input; input++; n2 = *input; coutn1,n2endl; ostream_iteratorint output(cout,**); *output=n1 + n2; coutendl; } 例2:#includeiostream #includevector #include algorithmusing namespace std; int main() { const int SIZE = 5; int a[SIZE] = {1,2,3,4,5 }; vectorint v (a,a+5); //构造函数 try { v.at(100) = 7; } catch( out_of_range e) { cout e.what() endl; } cout v.front() “,” v.back() endl; v.erase(v.begin()); ostream_iteratorint output(cout ,“*); copy (v.begin(),v.end(),output); v.erase( v.begin(),v.end()); //等效于 v.clear(); if( v

文档评论(0)

djdjix + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档