实验8 模板(P292)

实验目的和要求

1.能够使用C++模板机制定义重载函数。

2.能够实例化及使用模板函数。

3.能够实例化和使用模板类。

4.应用标准C++模板库(STL)通用算法和函数对象实现查找和排序。

实验内容

1.分析并调试下列程序,了解函数模板的使用。

  1. //sy8_1.cpp
  2. #include<iostream>
  3. using namespace std;
  4. template < class T>
  5. T max(T a,T b)
  6. {
  7. return a>b?a:b;
  8. }
  9. int max(int a,int b)
  10. {
  11. return a>b?a:b;
  12. }
  13. int main()
  14. {
  15. cout<< "max(6,5) is "<<max( 6, 5)<< endl;
  16. cout<< "max('6','5') is "<<max( 6, 5)<< endl;
  17. return 0;
  18. }

(1)写出运行结果,分析编译系统工作过程。

程序输出结果如下:


答:这个程序就是将a和b进行比较,输出两个数中大的数,主函数中 cout<<"max(6,5)is"<<max(6,5)<<endl;调用max函数,6比5大,因此输出6。 cout<<"max('6','5')is"<<max(6,5)<<endl;同理。

(2)如果定义函数重载,代码如下:

int max(int a,int b){return a>b?a:b;}

float max(float a,float b){return a>b?a:b;}

如果程序中有max('6','5');调用时会出现什么错误?为什么?上机调试并分析原因。

定义函数重载后程序如下:

  1. #include<iostream>
  2. using namespace std;
  3. template < class T>
  4. T max(T a,T b)
  5. { return a>b?a:b;}
  6. int max(int a,int b){ return a>b?a:b;}
  7. float max(float a,float b){ return a>b?a:b;}
  8. int main()
  9. {
  10. cout<< "max(6,5)is "<<max( 6, 5)<< endl;
  11. cout<< "max('6','5')is "<<max( 6, 5)<< endl;
  12. return 0;
  13. }

程序运行结果如下:


2.分析调试下列程序,了解特定模板函数的作用。

  1. //sy8_2.cpp
  2. #include <iostream>
  3. using namespace std;
  4. template < typename T>
  5. T max(T a,T b)
  6. {
  7. return a>b?a:b;}
  8. char* max(char *a,char *b)
  9. { return strcmp(a,b)> 0? a:b;}
  10. int main( ){
  11. cout<< "max(6,5) is "<<max( 6, 5)<< endl;
  12. cout<< "max(\"China\",\"Japan\") is "
  13. <<max( "China", "Japan")<< endl;
  14. return 0;
  15. }

(2) 说明特定模板函数的作用。

       答:特定模板函数的作用是可以替换原本会自动从函数模板中创建的模板实例,用来替换的函数。

3.声明一个类模板,利用它实现10个整数、浮点数和字符的排序。(sy8_3.cpp)

编写程序如下:

  1. //sy8_3.cpp
  2. #include <iostream>
  3. #include<cstdio>
  4. template < class T>
  5. class MySort
  6. {
  7. public:
  8. MySort(T _stores[ 10])
  9. {
  10. stores = _stores;
  11. }
  12. void DoSort(bool up = true)
  13. {
  14. for( int i = 0; i < 10; i++)
  15. {
  16. for( int j = i+ 1; j < 10; j++)
  17. {
  18. if(up) //升序
  19. {
  20. if(stores[i] > stores[j])
  21. {
  22. T temp = stores[i];
  23. stores[i] = stores[j];
  24. stores[j] = temp;
  25. }
  26. }
  27. else//降序
  28. {
  29. if(stores[i] < stores[j])
  30. {
  31. T temp = stores[i];
  32. stores[i] = stores[j];
  33. stores[j] = temp;
  34. }
  35. }
  36. }
  37. }
  38. }
  39. private:
  40. T *stores;
  41. };
  42. int main()
  43. {
  44. //int
  45. int nums[ 10] = { 1, 3, 2, 9, 7, 6, 8, 10, 4, 5};
  46. MySort< int> mySort(nums);
  47. mySort.DoSort( true); //升序
  48. for( int i = 0; i < 10; i++)
  49. printf( "%d ", nums[i]);
  50. printf( "\n");
  51. //float
  52. float nums1[ 10] = { 99.7f, 99.1f, 81.2f, 1.1f, 2.2f, 7.7f, 7.8f, 49.1f, 75.3f, 89.99f};
  53. MySort< float> mySort1(nums1);
  54. mySort1.DoSort( false);
  55. for( int i = 0; i < 10; i++)
  56. printf( "%0.2f ", nums1[i]);
  57. printf( "\n");
  58. // char
  59. char chars[ 10] = { 'a', 'A', 'B', 'b', 'z', 'X', 'W', 'e', 'g', 'H'};
  60. MySort< char> mySort2(chars);
  61. mySort2.DoSort();
  62. for( int i = 0; i < 10; i++)
  63. printf( "%c ", chars[i]);
  64. printf( "\n");
  65. return 0;
  66. }
程序输出结果入下:

4.声明一个整型数组,使用C++标准模板库(STL)中的查找算法find()进行数据的查找,然后应用排序算法sort()对数据进行升序和降序排序。(sy8_4.cpp)

编写程序如下:

  1. #include<iostream>
  2. #include<vector>
  3. #include<algorithm>
  4. using namespace std;
  5. bool largeThan(int x,int y)
  6. {
  7. return x>y;
  8. }
  9. int main()
  10. {
  11. int a[ 7]={ 4, 8, 3, 9, 5, 1, 7};
  12. size_t arrSize= 7;
  13. int searchValue= 5;
  14. vector< int> vec(a,a+arrSize);
  15. vector< int>::iterator it=find(vec.begin(),vec.end(),searchValue);
  16. if(it==vec.end())
  17. cout<< "not found"<< endl;
  18. else
  19. cout<<searchValue<< "'s index is "<<(it-vec.begin())<< endl;
  20. //升序
  21. sort(vec.begin(),vec.end());
  22. for( vector< int>::iterator it=vec.begin();it!=vec.end();it++)
  23. cout<<*it<<ends;
  24. cout<< endl;
  25. //降序;
  26. sort(vec.begin(),vec.end(),largeThan);
  27. for( vector< int>::iterator it=vec.begin();it!=vec.end();it++)
  28. cout<<*it<<ends;
  29. cout<< endl;
  30. }

程序输出结果如下:



分析与讨论

1.结合实验内容中第1题和第2题,说明编译器匹配函数的过程。

2.结合实验内容中第3题和第4题,比较利用自定义类模板排序和使用C++标准模板库排序的过程。

实验总结:
   在本次实验中我学会了使用C++模板机制定义重载函数和模板函数及模板类的使用。函数模板的重载有多种方式,可以定义同名的函数模板,提供不同的参数和实现,也可以用其他非模板函数重载。要注意的是在编译的过程中,编译器不会为没有用到的任何类型生成相应的模板函数,并且无论为一种类型使用了多少次函数模板,都只为该类型生成一个模板函数。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值