C++(1)_函数模板

函数模板

模板是C++泛型编程的基础。
一个模板就是创建一个类或者函数的蓝图或者说公式

假定我们希望编写一个函数来比较两个值,并指出第一个值是小于、等于还是大于第二个值。在实际中,我们可能想要定义多个函数,每个函数比较一种给定类型的值。
我们的初次尝试可能会定义多个重载函数:

//如果两个值相等,返回0,如果v1小返回-1,如果v2小返回1
int compare(const string &v1, const string &v2)
{
    if (v1 < v2) return -1;
    if (v2 < v1) return 1;
    return 0;
}
int compare(const double &v1, const double &v2)
{
    if (v1 < v2) return -1;
    if (v2 < v1) return 1;
    return 0;
}

对于不同类型的参数,不得不通过重载函数,定义完全一样的函数体,是非常繁琐且容易出错的。
更麻烦的是,在编写程序的时候,我们就要确定可能要compare的所有类型。如果希望能在用户提供的类型上使用此函数,这种策略就失效了。
函数模板的实质是不但让函数的参数值可以变化,而且还让参数的类型也可以变化。

一个函数模板就是一个公式,可用来生成针对特定类型的函数版本
  • compare的模板版本
template <typename T>
int compare(const T &v1, const T &v2)
{
    if (v1 < v2) return -1;
    if (v2 < v1) return 1;
    return 0;
}
//定义了一个名为T的类型参数,我们用名字T表示一个类型。而T表示的实际类型则在编译时根据compare的使用情况来确定。
  • 实例化
    在使用函数模板的时候,需要使模板参数实例化,实例化的目的是让函数参数的类型予以确定。实例化有两种方法:

    (1)、显式方法
    就是在使用之前,通过说明一个函数原型来确定函数参数的类型;

    (2)、隐式方法
    不要事先说明函数参数的类型,而是由函数在调用运行时确定参数的类型。
    例题1、设计一个函数模板,比较两个数的大小,当第一个数大于第二个数的时候返回1,否则返回0。

template <class  T1>
int isgreat (T1  x,  T1  y)
{  
    return  (x>y);
}

int  isgreat (float,  float  );  //显式实例化

void mian()
{  
    isgreat (2.5,  3.0);
    isgreat (3, 5);              //隐式实例化
    isgreat (10.0, 5.0 );
}

例题 2、比较两个数,返回最大值

#include <iostream.h>
#include <conio.h>
template  <class T>
T max(T x, T y)
{
    return (x>y ? x : y);
} 
void  main()
{ 
    int m1=max(100, 300);
    double m2=max(32.1,  43.0);
    char *m3=max(“zhen”, “li”);
}

//上述第三个语句有问题,原来要求按内容比较的,现在却变成了地址的直接比较,因此需要进行特殊处理,方法是从新定义一个同名的显式函数以替代函数模板。
#include <string.h>
char * max(char * x, char *y)
{
    return (strcmp(x,y)>0 ? x : y);
}
//在使用时,一般函数优先于函数模板。
//例题
#include  <iostream.h>
template  <class  T1, class  T2>
T1  max (T1 x, T2 y)
{
    return (x>y ? x : y);
} 

void  main()
{ 
    int h = 3;
    double g = 3.14; 
    cout<<”max=”<<max(h,g)<<endl;  //输出3
    cout<<”max=”<<max(g,h)<<endl;  //输出3.14
}

上述例题中的计算结果在数学上应该是一致的, 但在计算机中却得到两种计算结果,究其原因是由于函数中规定了返回值的类型由第一个参数决定,因此导致得到不同的计算结果。有的人也许会认为重载一个函数,让其返回值的类型改为T2不就行了吗?但是这样并不是函数重载。

第一次作业:
用函数模板求一维数组中的最大和最小值。

// Ex.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

template  <typename T>
//BubbleSort 升序
void sort (T *a, int n)
{
    T temp = a[0];
    for(int i=1; i<n; i++)
    {
        for(int j=n-1; j>=i; j--)
        {
            if (a[j] < a[j-1])
            {
                temp = a[j-1];
                a[j-1] = a[j];
                a[j] = temp;
            }
        }
    }
}

void  main()
{ 
    int a[] = {1, 2, 3, 4, 5};
    double b[] = {1.0, 2.0, 3.0, 4.0, 5.0}; 
    char c[] = {'a', 'b', 'c', 'd', 'e'};
    string d[] = {"abc", "abd", "abe", "abf", "abg"};

    sort(a,5);
    cout<<"min for an int array: "<<a[0]<<endl;
    cout<<"max for an int array: "<<a[4]<<endl;

    sort(b,5);
    //发现若小数部分为0,输出不带小数点
    //为了显示小数点,需要有showpoint
    cout<<showpoint<<"min for a double array(with point): "<<b[0]<<endl;
    cout<<showpoint<<"max for a double array(with point): "<<b[4]<<endl;

    sort(c,5);
    cout<<"min for a char array: "<<c[0]<<endl;
    cout<<"max for a char array: "<<c[4]<<endl;

    sort(d,5);
    cout<<"min for a string array: "<<d[0]<<endl;
    cout<<"max for a string array: "<<d[4]<<endl;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值