1 / 17

第 6 章 泛型算法初步

第 6 章 泛型算法初步. 6. 内容提要. 本章介绍了泛型算法的基本概念。 介绍了使用泛型算法的必要性,泛型算法的组成、以及反向 iterator 、 istream_iterator 和 ostream_iterator 的使用方法。 介绍了如何在数组和容器类型中使用泛型算法。. 泛型算法的必要性.

newton
Download Presentation

第 6 章 泛型算法初步

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第6章 泛型算法初步 6

  2. 内容提要 • 本章介绍了泛型算法的基本概念。 • 介绍了使用泛型算法的必要性,泛型算法的组成、以及反向iterator、istream_iterator和ostream_iterator的使用方法。 • 介绍了如何在数组和容器类型中使用泛型算法。

  3. 泛型算法的必要性 • 在进行程序设计的时候,一些常用的算法经常用到。比如:取最大值(max())、取最小值(min())、查扎(find())和排序(sort()),在调用这些算法的时候,希望算法并不局限于某一种数据类型,比如不管是vector、list还是内置的数组类型都可以使用。 • 为了实现这些需要,需要引入“泛型算法”,所谓“泛型”是它们的操作是建立多种容器上的,所谓“算法”是因为实现的是公共操作,比如:min()、max()和sort()等等。使用“泛型算法”可以大大提高代码的开发效率。

  4. 在vector中查找一个数 案例名称:在vector中查找一个数 程序名称:proj6_01.cpp #include <vector> #include <iostream.h> using namespace std; void main() { int iSearchValue = 47; int ia[ 6 ] = { 27, 210, 12, 47, 109, 83 }; vector<int> ivec( ia, ia+6 ); vector <int>::iterator iter = ivec.begin(); for (; iter != ivec.end(); ++iter) { if (*iter == iSearchValue) cout << "找到要查找的字符了!" << endl; } }

  5. 泛型算法的组成 • 对于find()算法,一般性步骤是: • 1、顺次检查每个元素 • 2、如果当前元素等于被检查的值,那么返回该元素在集合中的位置。 • 3、否则,检查下一个元素,重复步骤2,直到找到一个元素,或者检查完所有元素。 • 4、如果已经到了集合的末尾,而且没有找到该值,则返回该值在这个集合中不存在。

  6. 在普通数组中使用泛型算法 案例名称:在普通数组中使用泛型算法 程序名称:proj6_02.cpp #include <algorithm> #include <vector> #include <iostream.h> using namespace std; void main() { int search_value; int ia[ 6 ] = { 27, 210, 12, 47, 109, 83 }; search_value = 12; vector<int>::iterator presult; presult = find( &ia[0], &ia[6], search_value ); cout << "The value " << search_value << (presult == &ia[6] ? " is not present" : " is present" ) << endl; }

  7. 在vector中使用泛型算法 案例名称:在vector中使用泛型算法 程序名称:proj6_03.cpp #include <algorithm> #include <vector> #include <iostream.h> using namespace std; void main() { int search_value; int ia[ 6 ] = { 27, 210, 12, 47, 109, 83 }; vector<int> vec( ia, ia+6 ); search_value = 20; vector<int>::iterator presult; presult = find( vec.begin(), vec.end(), search_value ); cout << "The value " << search_value << (presult == vec.end() ? " is not present" : " is present" ) << endl; }

  8. 在list中使用泛型算法 案例名称:在list中使用泛型算法 程序名称:proj6_04.cpp #include <algorithm> #include <list> #include <iostream.h> using namespace std; void main() { int search_value; int ia[ 6 ] = { 27, 210, 12, 47, 109, 83 }; list<int> ilist( ia, ia+6 ); search_value = 20; list<int>::iterator presult; presult = find( ilist.begin(), ilist.end(), search_value ); cout << "The value " << search_value << (presult == ilist.end() ? " is not present" : " is present" ) << endl; }

  9. 几种常用的iterator • 除了上面介绍的iterator,还有反向iterator、iostream_iterator、istream_iterator和ostream_iterator。 • 这几种都比较常用。

  10. 反向iterator • 反向iterator的遍历方式同前向iterator一样。不同的是对于前向iterator,++操作访问容器中的下一个元素,对于反向iterator,它访问的是前面的元素。 • 例如,反向遍历一个vector,可以这样编写。 • vector <type> :: reverse_iterator r_iter • for ( r_iter = vec.rbegin(); //将r_iter绑定到末元素 • r_iter != vec.rend(); //不等于首元素下一元素 • r_iter++) //递减iterator一个元素

  11. 使用反向iterator 案例名称:使用反向iterator 程序名称:proj6_05.cpp #include <algorithm> #include <vector> #include <iostream.h> using namespace std; void main() { int ia[ 6 ] = { 27, 210, 12, 47, 109, 83 }; vector <int> vec( ia, ia+6 ); vector <int>::reverse_iterator r_iter; for ( r_iter = vec.rbegin(); r_iter != vec.rend(); r_iter++) { cout << * r_iter << endl; } }

  12. 使用反向iterator 案例名称:使用反向iterator 程序名称:proj6_06.cpp #include <algorithm> #include <vector> #include <iostream.h> using namespace std; void main() { int ia[ 6 ] = { 27, 210, 12, 47, 109, 83 }; vector <int> vec( ia, ia+6 ); vector <int>::iterator iter1; sort( vec.begin(), vec.end()); cout << "正序排列:" << endl; for ( iter1 = vec.begin(); iter1 != vec.end(); iter1++) { cout << * iter1 << " " ; } sort( vec.rbegin(), vec.rend()); cout << endl << "倒序排列:" <<endl; vector <int>::iterator iter2; for ( iter2 = vec.begin(); iter2 != vec.end(); iter2++) { cout << * iter2 << " "; } cout << endl; }

  13. istream_iterator和ostream_iterator • 标准库为输入和输出iostream的iterator提供了支持,它们可以与标准库容器类型和泛型算法结合起来工作。 • 分为两种iterator,一种是istream_iterator,另一种是ostream_iterator,为了使用这两种iterator,必须使用头文件“#include <iterator>”

  14. 使用istream_iterator和ostream_iterator 案例名称:使用istream_iterator和ostream_iterator 程序名称:proj6_07.cpp #include <iostream> #include <iterator> #include <algorithm> #include <vector> using namespace std; void main() { istream_iterator< int > input( cin ); istream_iterator< int > end_of_stream; vector<int> vec; copy ( input, end_of_stream, inserter( vec, vec.begin() )); sort( vec.begin(), vec.end()); ostream_iterator< int > output( cout, " " ); unique_copy( vec.begin(), vec.end(), output ); }

  15. 小结 • 本章需要了解使用泛型算法的必要性:泛型算法提高了程序的编写效率。 • 了解泛型算法的组成,熟练掌握在数组类型、vector类型和list类型中使用泛型算法函数。 • 掌握泛型算法函数find()、copy()、sort()的使用方法。 • 熟悉几种常用的iterator:反向iterator、istream_iterator和ostream_iterator。

  16. 本章习题 • 一、选择题 • 1、使用泛型算法的作用是___________。(选择两项) • A) 提高编写效率B) 提高程序设计的难度 • C) 提高算法的通用性D) 没有什么作用 • 2、下面的数据类型,哪个不能使用泛型算法___________。 • A) list lst; B) int a[10] • C) int b; D) vector vec; • 3、阅读程序: • int ia[ 6 ] = { 27, 210, 12, 47, 109, 83 }; • vector <int> vec( ia, ia+6 ); • vector <int>::reverse_iterator r_iter; • for ( r_iter = vec.rbegin(); r_iter != vec.rend(); r_iter++) • { • cout << * r_iter << " " • } • 程序输出的结果为:___________。 • A) 27 210 12 47 109 83 B) 83 109 47 12 210 27 • C) 12 27 47 83 109 210 D) 210 109 83 47 27 12

  17. 本章习题 • 1、泛型算法中的find()函数定义在头文件___________中,因此需要引入该头文件。 • 2、程序填空: • int ia[ 6 ] = { 27, 210, 12, 47, 109, 83 }; • vector <int> vec( ia, ia+6 ); • ___________ • sort( vec.begin(), vec.end()); • cout << "正序排列:" << endl; • for ( iter1 =___________; iter1 != vec.end(); iter1++) • { • cout << * iter1 << " " ; • } • 3、为了使用四个算法——adjacent_difference()、accumulate()、inner_product()和partial_sum(),必须包含“___________” • 4、两种iterator,一种是istream_iterator,另一种是ostream_iterator,为了使用这两种iterator,必须使用头文件“___________”

More Related