C++ 函数模板

一个函数是不是只能用固定的数据类型呢?答案是否定的。只要是遇到处理完全相同,但是类型不一样的函数就可以使用函数模板。函数模板可以增加代码重用率,它使用非特定类型定义函数,到使用的时候再使用特定的类型替代。在没有函数模板的情况下,当我们需要对多种不同的数据类型进行相同的操作时,只能使用函数重载的方法,定义出多个函数。例子如下:

//函数重载
#include <iostream>

int min(int ii, int jj, int kk)
{
    int temp;
    if ((ii < jj) && (ii < jj))
    {
        temp = ii;
    }
    else if ((jj < ii) && (jj < kk))
    {
        temp = jj;
    }
    else
    {
        temp = kk;
    }
    return temp;

}


double min(double ii, double jj, double kk)
{
    double temp;
    if ((ii < jj) && (ii < jj))
    {
        temp = ii;

    }
    else if ((jj < ii) && (jj < kk))
    {
        temp = jj;
    }
    else
    {
        temp = kk;
    }

    return temp;
}


float min(float ii, float jj, float kk)
{
    float temp;
    if ((ii < jj) && (ii < jj))
    {
        temp = ii;

    }
    else if ((jj < ii) && (jj < kk))
    {
        temp = jj;
    }
    else
    {
        temp = kk;
    }

    return temp;
}

int main()
{
    int temp1 = min(100, 20, 30);
    std::cout << temp1 << std::endl;
    double temp2 = min(10.70, 10.64, 53.21);
    std::cout << temp2 << std::endl;

    float temp3 = min(10.70f, 10.64f, 53.21f);
    std::cout << temp2 << std::endl;
}

使用函数模板后,可以将代码改写成如下形式

#include <iostream>
//使用函数模板,可以大大减少代码量
template <class T>
T min(T ii, T jj, T kk)
{
    T temp ;
    if ((ii < jj) && (ii < jj))
    {
        temp = ii;
    }
    else if ((jj < ii) && (jj < kk))
    {
        temp = jj;
    }
    else
    {
        temp = kk;
    }
    return temp;
}

int main()
{
    std::cout << min(100, 20, 30) << std::endl;
    std::cout << min(10.60, 10.56, 53.21) << std::endl;
    std::cout << min(10.60f, 10.56f, 53.21f) << std::endl;
}

函数模板还可以进行重载,特别是对于原模板函数存在缺陷的情况下,可以对函数进行定制。例子如下:

#include <iostream>
#include <cstring>
template <class T>
T min(T ii, T jj, T kk)
{

    T temp ;
    if ((ii < jj) && (ii < jj))
    {
        temp = ii;
    }
    else if ((jj < ii) && (jj < kk))
    {
        temp = jj;

    }
    else
    {
        temp = kk;
    }

    return temp;

}

const char *min (const char *ch1, const char *ch2, const char *ch3)
{

    const char *temp;

    int result1 = strcmp(ch1, ch2);

    int result2 = strcmp(ch1, ch3);

    int result3 = strcmp(ch2, ch1);

    int result4 = strcmp(ch2, ch3);

    if ((result1 < 0) && (result2 < 0))
    {
        temp = ch1;

    }

    else if ((result3 < 0) && (result4 < 0))
    {
        temp = ch2;
    }


    else
    {
        temp = ch3;
    }

    return temp;
}
int main()
{
    std::cout << min(100, 20, 30) << std::endl;
    std::cout << min(10.60, 10.56, 53.21) << std::endl;
    std::cout << min(10.60f, 10.56f, 53.21f) << std::endl;
    std::cout << min("Anderson", "Washington", "Smith") << std::endl;

}

对有缺陷的函数模板,使用同名非模板函数进行重载,这种操作称为函数定制

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值