c++新特性-------函数包装器,模板元加速

#include <algorithm>
#include <iostream>
#include <functional>
#include <vector>
#include <numeric>
#include <array>
#include <cstring>
#include <cstdio>
#include <functional>//包装头文件
using namespace  std;
using namespace std::function;
void go()
{
    cout << "go" << endl;
}
int add(int a, int b)
{
    return a + b;
}
int main()
{
            //返回值 参数
    function<void(void)> fun1 = go;
    fun1();
    function<int(int, int)> fun2 = add;
    cout << fun2(10,19) << endl;
    function<int(int, int)> fun3 = [](int a, int b)->int{return a + b; };
    cout << fun4(10, 19) << endl;
}

具体可看:https://www.cnblogs.com/wangkeqin/p/9338358.html

 

 

模板元加速

 

#include <algorithm>
#include <iostream>
#include <functional>
#include <vector>
#include <numeric>
#include <array>
#include <cstring>
#include <cstdio>
#include <functional>//包装头文件
using namespace  std;

int  get50(int n)
{
    if (n==1)
    {
        return 1;
    } 
    else if (n==2)
    {
        return 2;
    }
    else
    {
        return get50(n-1)+get50(n-2);
    }
}
//模板元用于实现递归加速,递归:反复调用,函数等待,返回,浪费时间比较多
//执行速度快,编译的时候慢,代码提交会增加
//把运行的时间节约在编译的时候,用于递归加速,游戏的优化,目前仅使用与c++11
template<int N>
struct data
{
    enum 
    {
        res = data<N - 1>::res + data<N - 2>::res
    };
};
template<>
struct data<1>
{
    enum 
    {
        res=1
    };
};
template<>
struct data<2>
{
    enum
    {
        res = 2
    };
};

int main()
{    
    cout << data<40>::res << endl;//模板元用于处理代码加速,只支持字面量、常量,不支持变量
    cout << get50(40) << endl;//速度很慢
    
    system("pause");
}

 

转载于:https://www.cnblogs.com/bwbfight/p/11299333.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值