实验目的和要求

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

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

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

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


实验内容

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

[cpp] view plain copy
  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)写出运行结果,分析编译系统工作过程。

实验八 模板

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

[cpp] view plain copy
  1. int max(int a,int b){return a>b?a:b;}  
  2. float max(float a,float b){return a>b?a:b;}   

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

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

[cpp] view plain copy
  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. }  
  9. char *max(char *a,char *b)  
  10. {  
  11.     return strcmp(a,b)>0?a:b;  
  12. }  
  13. int main()  
  14. {  
  15.     cout<<"max(6,5)is"<<max(6,5)<<endl;  
  16.     cout<<"max(\"China\",\"Japan\")is"<<max("China","Japan")<<endl;  
  17.     return 0;  
  18. }  

(1)写出运行结果。

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


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

[cpp] view plain copy
  1. //sy8_3.cpp  
  2. #include<iostream>  
  3. using namespace std;  
  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.   
  43. int main()  
  44. {  
  45.     //int  
  46.     int nums[10];  
  47.    cout<<"输入10个整数:"<<nums[10]<<endl;  
  48.     for(int i = 0;i<10;i++)  
  49.        cin>> nums[i];  
  50.     MySort<int> mySort(nums);  
  51.     mySort.DoSort(true);//升序  
  52.     for(int i = 0; i < 10; i++)  
  53.         cout<<nums[i]<<endl;  
  54.   
  55.     //float  
  56.     float nums1[10];  
  57.     cout<<"输入10个浮点数:"<<nums1[10]<<endl;  
  58.     for(int i = 0; i < 10; i++)  
  59.        cin>>nums1[i];  
  60.     MySort<float> mySort1(nums1);  
  61.     mySort1.DoSort(false);  
  62.     for(int i = 0; i < 10; i++)  
  63.          cout<<nums1[i]<<endl;  
  64.   
  65.     // char  
  66.     char chars[10];  
  67.     cout<<"输入10个字符:"<<chars[10]<<endl;  
  68.     for(int i = 0; i < 10; i++)  
  69.        cin>>chars[i];  
  70.     MySort<char> mySort2(chars);  
  71.     mySort2.DoSort();  
  72.     for(int i = 0; i < 10; i++)  
  73.         cout<<chars[i]<<endl;  
  74.   
  75.     return 0;  
  76. }  
运行结果如下:

实验八 模板

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

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

实验八 模板

分析与讨论

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

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

相关文章: