前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >小朋友学C++(19):函数模板

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

作者头像
海天一树
发布2018-07-25 13:05:32
2730
发布2018-07-25 13:05:32
举报
文章被收录于专栏:海天一树海天一树

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

代码语言:javascript
复制
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型的和

代码语言:javascript
复制
#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;
}

运行结果:

代码语言:javascript
复制
3
3.3
3.3333333333

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

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

代码语言:javascript
复制
#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;
}

运行结果:

代码语言:javascript
复制
3
3.3
3.3333333333

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

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

代码语言:javascript
复制
#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;
}

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

代码语言:javascript
复制
#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;
}

运行结果:

代码语言:javascript
复制
0.01
225
2.32305729

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-06-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 海天一树 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档