标准模板库(二)_第1页
标准模板库(二)_第2页
标准模板库(二)_第3页
标准模板库(二)_第4页
标准模板库(二)_第5页
已阅读5页,还剩32页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、程序设计实习,第十九讲 标准模板库(二),本讲作业: 写一个自己的 CMyostream_iterator 模板,使之能和 ostream_iterator 模板达到一样的效果,即 #include #include #include using namespace std; main() int a5 = 1,2,3,4,5; CMyostream_iterator output(cout,*); vector v(a,a+5); copy(v.begin(),v.end(),output); ,程序的输出结果是: 1*2*3*4*5* 注意,编写CMyostream_iterator时不能

2、使用 ostream_iterator,注意,编写CMyostream_iterator时不能使用 ostream_iterator 参考 copy 的help copy template OutIt copy(InIt first, InIt last, OutIt x); The template function evaluates *(x + N) = *(first + N) once for each N in the range 0, last - first), for strictly increasing values of N beginning with the low

3、est value. It then returns x + N. If x and first designate regions of storage, x must not be in the range first, last) copy 的源代码: template inline _OI copy(_II _F, _II _L, _OI _X) for (; _F != _L; +_X, +_F) *_X = *_F; return (_X); ,4.2 list 容器 在任何位置插入删除都是常数时间,不支持随机存取 除了具有所有顺序容器都有的成员函数以外,还支持8个成员函数: pu

4、sh_front: 在前面插入 pop_front: 删除前面的元素 sort: 排序 ( list 不支持 STL 的算法 sort) remove: 删除和指定值相等的所有元素 unique:删除所有和前一个元素相同的元素 merge: 合并两个链表,并清空被合并的那个 reverse: 颠倒链表,splice: 在指定位置前面插入另一链表中的一个或多个元素,并在另一链表中删除被插入的元素,#include #include #include using std:list; using std:ostream; using std:cout; using std:endl; class A

5、 private: int n; public: A( int n_ ) n = n_; friend bool operator( const A ,bool operator( const A ,template void PrintList(const std:list ,main() list lst1,lst2; lst1.push_back(1);lst1.push_back(3); lst1.push_back(2);lst1.push_back(4); lst1.push_back(2); lst2.push_back(10);lst2.push_front(20); lst2

6、.push_back(30);lst2.push_back(30); lst2.push_back(30);lst2.push_front(40);lst2.push_back(40); cout 1) ; PrintList( lst1); cout endl; cout 2) ; PrintList( lst2); cout endl; lst2.sort(); cout 3) ; PrintList( lst2); cout endl; lst2.pop_front(); cout 4) ; PrintList( lst2); cout endl;,lst1.remove(2);/删除所

7、有和A(2)相等的元素 cout 5) ; PrintList( lst1); cout endl; lst2.unique(); /删除所有和前一个元素相等的元素 cout 6) ; PrintList( lst2); cout endl; lst1.merge (lst2); /合并 lst2到lst1并清空lst2 cout 7) ; PrintList( lst1); cout endl; cout 8) ; PrintList( lst2); cout endl; lst1.reverse(); cout 9) ; PrintList( lst1); cout endl; lst2.

8、push_back (100);lst2.push_back (200); lst2.push_back (300);lst2.push_back (400);,std:list:iterator p1,p2,p3; p1 = std:find(lst1.begin(),lst1.end(),3); p2 = std:find(lst2.begin(),lst2.end(),200); p3 = std:find(lst2.begin(),lst2.end(),400); lst1.splice(p1,lst2,p2, p3); /将p2,p3)插入p1之前, /并从lst2中删除p2,p3)

9、 cout 11) ; PrintList( lst1); cout endl; cout 12) ; PrintList( lst2); cout endl; ,输出: 1) 1,3,2,4,2, 2) 40,20,10,30,30,30,40, 3) 10,20,30,30,30,40,40, 4) 20,30,30,30,40,40, 5) 1,3,4, 6) 20,30,40, 7) 1,3,4,20,30,40, 8) 9) 40,30,20,4,3,1, 11) 40,30,20,4,200,300,3,1, 12) 100,400,4.3 deque 容器 所有适用于 vecto

10、r的操作都适用于 deque deque还有 push_front(将元素插入到前面) 和 pop_front(删除最前面的元素)操作,5 函数对象 是个对象,但是用起来看上去象函数调用,实际上也执行了函数调用 class CMyAverage public: double operator()( int a1, int a2, int a3 ) /重载 () 运算符 return (double)(a1 + a2+a3) / 3; ; / 重载 () 运算符时,参数可以是任意多个 CMyAverage Average; /函数对象 cout Average(3,2,3); / Average

11、.operator(3,2,3) 用起来看上去象函数调用 输出 2.66667,函数对象的应用: STL里有以下模板: template T accumulate(InIt first, InIt last, T val, Pred pr); pr 就是个函数对象,实际上是个函数也可以 对first,last)中的每个迭代器 I, 执行 val = pr(val,* I) ,返回最终的val,#include #include #include #include #include using namespace std; int sumSquares( int total, int value

12、) return total + value * value; ,template class SumSquaresClass public: const T ,int main() const int SIZE = 10;int a1 = 1, 2,3,4,5,6,7,8,9,10 ; vector v(a1,a1+SIZE); ostream_iterator output(cout, ); cout s; result = accumulate(v.begin(),v.end(),0,s); / (1) cout 3) 平方和: result; ,输出: 1) 1 2 3 4 5 6 7

13、 8 9 10 2) 平方和:385 3) 平方和:385,课本上是: class SumSquaresClass:public binary_function public: const T /(1) 效果一样,binary_function定义: template struct binary_function typedef Arg1 first_argument_type; typedef Arg2 second_argument_type; typedef Result result_type; ;,STL 的 里还有以下函数对象类模板(P750): divides equal_to

14、greater less . 这些模板可以用来生成函数对象,greater 函数对象类模板 template struct greater : public binary_function bool operator()(const T,greater 的应用: list 有两个sort函数,前面看到的是不带参数的sort函数,它将list按 pr); 可以用来进行降序排序,#include #include using namespace std; main() const int SIZE = 5; int aSIZE = 5,1,4,2,3; list lst(a,a+SIZE); ls

15、t.sort(greater();/greater()是个对象 /本句进行降序排序 ostream_iterator output(cout,); copy( lst.begin(),lst.end(),output); cout endl; /输出:5,4,3,2,1,6 关联容器 set, multiset, map, multimap 内部元素有序排列,新元素插入的位置取决于它的值,查找速度快 除了各容器都有的函数外,还支持以下成员函数: find: 查找 lower_bound upper_bound count :计算等于某个值的元素个数 插入元素用 insert,6.1 multi

16、set 定义:template, class A = allocator class multiset ; 第二个参数 Pred 是个函数对象 Pred决定了multiset 中的元素,“一个比另一个小”是怎么定义的,即 Pred(x,y) 如果返回值为true,则 x比y小 Pred的缺省值是 less less 模板的定义: template struct less : public binary_function bool operator()(const T /less模板是靠 来比较大小的,multiset 的用法: class A . ; multiset a; 就等效于 mult

17、iset a; 由于less模板是用 进行比较的,所以 这都要求 A 的对象能用 比较,即适当重载了 ,/出错的例子: #include using namespace std; class A ; main() multiset a; a.insert( A(); /error /编译出错是因为,插入元素时,multiset会将被插入元素和已有元素进行比较,以决定新元素的存放位置。本例中缺省地就是用less函数对象进行比较,然而less函数对象进行比较时,前提是A对象能用 进行比较。但本例中没有适当重载 ,从 begin() 到 end()遍历一个 multiset对象,就是从小到大遍历各个

18、元素 例子程序:,#include #include using namespace std; class MyLess;,class A private:int n; public: A(int n_ ) n = n_; friend bool operator ( const A ,class MyLess public: bool operator()( const A / MSET2 里,元素的排序规则与 MSET1不同, /假设 le 是一个 MyLess对象,a1和a2是MSET2对象 /里的元素,那么, le(a1,a2) = true 就说明 a1比a2小,main() const int SIZE = 5; A aSIZE = 4,22,19,8,33 ; ost

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论