实验八

实验目的和要求

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

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

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

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

实验内容

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

#include<iostream>  
using namespace std;  
template <class T>  
T max(T a,T b)  
{  
    return a>b?a:b;  
}  
int main()  
{  
    cout<<"max(6,5)is"<<max(6,5)<<endl;  
    cout<<"max('6','5')is"<<max(6,5)<<endl;  
    return 0;  
}

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

(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');调用时会出现什么错误?为什么?上机调试并分析原因。

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

#include<iostream>    
using namespace std;    
template <class T>    
T max(T a,T b)    
{return a>b?a:b;}    
    int max(int a,int b){return a>b?a:b;}    
    float max(float a,float b){return a>b?a:b;}    
    
int main()    
{    
    cout<<"max(6,5)is "<<max(6,5)<<endl;    
    cout<<"max('6','5')is "<<max(6,5)<<endl;    
    return 0;    
} 


程序运行结果如下:

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

#include<iostream>  
using namespace std;  
template<typename T>  
T max(T a,T b)  
{  
   return a>b?a:b;  
}  
char *max(char *a,char *b)  
{  
    return strcmp(a,b)>0?a:b;  
}  
int main()  
{  
    cout<<"max(6,5)is"<<max(6,5)<<endl;  
    cout<<"max(\"China\",\"Japan\")is"<<max("China","Japan")<<endl;  
    return 0;  
}
(2) 说明特定模板函数的作用。

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

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

编写程序如下:


#include <iostream>    
#include<cstdio>  
template <class T>    
class MySort    
{    
public:    
    MySort(T _stores[10])    
    {    
        stores = _stores;    
    }    
    void DoSort(bool up = true)    
    {    
        for(int i = 0; i < 10; i++)    
        {    
            for(int j = i+1; j < 10; j++)    
            {    
                if(up)//升序    
                {    
                    if(stores[i] > stores[j])    
                    {    
                        T temp = stores[i];    
                        stores[i] = stores[j];    
                        stores[j] = temp;    
                    }    
                }    
                else//降序    
                {    
                    if(stores[i] < stores[j])    
                    {    
                        T temp = stores[i];    
                        stores[i] = stores[j];    
                        stores[j] = temp;    
                    }    
                }    
            }    
        }    
    }    
private:    
    T *stores;    
};    
    
int main()    
{    
    //int    
    int nums[10] = { 1, 3, 2, 9, 7, 6, 8, 10, 4, 5};    
    MySort<int> mySort(nums);    
    mySort.DoSort(true);//升序    
    for(int i = 0; i < 10; i++)    
        printf("%d ", nums[i]);    
    printf("\n");    
    //float    
    float nums1[10] = { 99.7f, 99.1f, 81.2f, 1.1f, 2.2f, 7.7f, 7.8f, 49.1f, 75.3f, 89.99f};    
    MySort<float> mySort1(nums1);    
    mySort1.DoSort(false);    
    for(int i = 0; i < 10; i++)    
        printf("%0.2f ", nums1[i]);    
    printf("\n");    
    // char    
    char chars[10] = {'a', 'A', 'B', 'b', 'z', 'X', 'W', 'e', 'g', 'H'};    
    MySort<char> mySort2(chars);    
    mySort2.DoSort();    
    for(int i = 0; i < 10; i++)    
        printf("%c ", chars[i]);    
    printf("\n");    
    return 0;    
}

程序输出结果入下:

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

编写程序如下:


#include<iostream>  
#include<vector>  
#include<algorithm>  
using namespace std;  
  
bool  largeThan(int x,int y)  
{  
    return x>y;  
}  
int main()  
{  
    int a[7]={4,8,3,9,5,1,7};  
    size_t arrSize=7;  
    int searchValue=5;  
    vector<int> vec(a,a+arrSize);  
    vector<int>::iterator it=find(vec.begin(),vec.end(),searchValue);  
    if(it==vec.end())  
        cout<<"not found"<<endl;  
    else  
        cout<<searchValue<<"'s index is "<<(it-vec.begin())<<endl;  
    //升序  
    sort(vec.begin(),vec.end());  
    for(vector<int>::iterator it=vec.begin();it!=vec.end();it++)  
        cout<<*it<<ends;  
    cout<<endl;  
  
    //降序;  
    sort(vec.begin(),vec.end(),largeThan);  
    for(vector<int>::iterator it=vec.begin();it!=vec.end();it++)  
        cout<<*it<<ends;  
    cout<<endl;  
}

程序输出结果如下:


分析与讨论

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

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

实验总结:

   本次实验要求我们能够使用C++模板机制定义重载函数,能够实例化及使用模板函数,能够实例化和使用模板类,以及应用标准C++模板库(STL)通用算法和函数对象实现查找和排序。实验过程中,遇到了很多问题,有些老师上课时讲过,但没理解,问了两位同学把问题解决,但还有些不能理解导致实验没有完全做完。


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值