小朋友学C++(19):函数模板

先看一段微软实现math.h中求幂的源码

template<class _Ty> inline
        _Ty _Pow_int(_Ty _X, int _Y)
        {unsigned int _N;
        if (_Y >= 0)
                _N = _Y;
        else
                _N = -_Y;
        for (_Ty _Z = _Ty(1); ; _X *= _X)
                {if ((_N & 1) != 0)
                        _Z *= _X;
                if ((_N >>= 1) == 0)
                        return (_Y < 0 ? _Ty(1) / _Z : _Z); }}

这里template表示模板。

在了解模板之前,咱们先来求一下两个 int型的和,两个float型的和,两个double型的和

#include <iostream>
#include <iomanip>

using namespace std;

int sum(int x, int y)
{
    return x + y;
}

float sum(float x, float y)
{
    return x + y;
}

double sum(double x, double y)
{
    return x + y;
}

int main()
{
    int a = 1, b = 2;
    // 调用int sum(int, int)
    cout << sum(a, b) << endl;

    float c = 1.1, d = 2.2;
    // 调用float sum(float, float)
    cout << sum(c, d) << endl;

    double e = 1.1111111111, f = 2.2222222222;
    // 调用double sum(double, double)
    cout << fixed << setprecision(10) << sum(e, f) << endl;

    return 0;
}

运行结果:

3
3.3
3.3333333333

分析:这里定义了三种类型的sum函数,假如只定义了int型的sum函数,那么编译的时候碰到sum(c, d)和sum(e, f)会报错。因为编译器找不到float和double的sum函数。

上面三个sum函数,除了函数返回类型和参数类型不一样外,功能是一样的,那么有没有办法使用一个通用的函数,就能同时满足int型、float型、double型的求和功能呢?
答案是有的。这就要用到函数模板。

#include <iostream>
#include <iomanip>

using namespace std;

template<class T>
T sum(T x, T y)
{
    return x + y;
}

int main()
{
    int a = 1, b = 2;
    // 调用int sum(int, int)
    cout << sum(a, b) << endl;

    float c = 1.1, d = 2.2;
    // 调用float sum(float, float)
    cout << sum(c, d) << endl;

    double e = 1.1111111111, f = 2.2222222222;
    // 调用double sum(double, double)
    cout << fixed << setprecision(10) << sum(e, f) << endl;

    return 0;
}

运行结果:

3
3.3
3.3333333333

分析:这个程序的关键是template,表示用了函数模板。class是关键字,代表着某种类型,这种类型在编译的时候,会根据被调用的参数而自动匹配。碰到int就是int型,碰到float就是float型,碰到double就是double型。

除了class关键字,还可以使用typename关键字,效果完全一样:

#include <iostream>
#include <iomanip>

using namespace std;

template<typename T>
T sum(T x, T y)
{
    return x + y;
}

int main()
{
    int a = 1, b = 2;
    // 调用int sum(int, int)
    cout << sum(a, b) << endl;

    float c = 1.1, d = 2.2;
    // 调用float sum(float, float)
    cout << sum(c, d) << endl;

    double e = 1.1111111111, f = 2.2222222222;
    // 调用double sum(double, double)
    cout << fixed << setprecision(10) << sum(e, f) << endl;

    return 0;
}

最后,咱们测试一下开头展示的快速幂函数:

#include <iostream>
#include <iomanip>

using namespace std;

template<class _Ty>
_Ty _Pow_int(_Ty _X, int _Y)
{
    unsigned int _N;
    if (_Y >= 0)
    {
        _N = _Y;
    }
    else
    {
        _N = -_Y;
    }

    for (_Ty _Z = _Ty(1); ; _X *= _X)
    {
        if ((_N & 1) != 0)
        {
            _Z *= _X;
        }

        if ((_N >>= 1) == 0)
        {
            return (_Y < 0 ? _Ty(1) / _Z : _Z);
        }
    }
}

int main()
{
    float a = 10;
    cout << _Pow_int(a, -2) << endl;

    int b = 15;
    cout << _Pow_int(b, 2) << endl;

    double c = 1.11111111;
    cout << fixed << setprecision(8) << _Pow_int(c, 8) << endl;

    return 0;
}

运行结果:

0.01
225
2.32305729


TopCoder & Codeforces & AtCoder交流QQ群:648202993
更多内容请关注微信公众号
wechat_public_header.jpg

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值