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;
}

模板元加速

#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");
}

猜你喜欢

转载自www.cnblogs.com/bwbfight/p/11299333.html