zoukankan      html  css  js  c++  java
  • 模板元编程

    //模板元把运行时消耗的时间,在编译期间优化

    //递归极其消耗时间

     1 #include <iostream>
     2 
     3 //模板元把运行时消耗的时间,在编译期间优化
     4 //递归极其消耗时间
     5 
     6 template <int N>
     7 struct data
     8 {
     9     enum { res = data<N - 1>::res + data<N - 2>::res };
    10 };
    11 
    12 template <>
    13 struct data<1>
    14 {
    15     enum { res = 1 };
    16 };
    17 
    18 template <>
    19 struct data<2>
    20 {
    21     enum { res = 1 };
    22 };
    23 
    24 int getdata(int n)//递归函数,费波拉契数列
    25 {
    26     if (n == 1 || n == 2)
    27     {
    28         return 1;
    29     }
    30     else
    31     {
    32         return getdata(n - 1) + getdata(n - 2);
    33     }
    34 }
    35 
    36 void main()
    37 {
    38     const int myint(45);
    39 
    40     //<不可以有变量,必须是常量>
    41     int num = data<myint>::res;//模板元编程,快
    42 
    43     std::cout << num << std::endl;
    44 
    45     std::cout << getdata(45) << std::endl;//递归函数,慢
    46     
    47     system("pause");
    48 }
  • 相关阅读:
    委托
    apply()和call()
    Sql小技巧
    plsql中文乱码
    Windows8中使用IE8等低版本浏览器
    React Native
    谷歌浏览器添加flash白名单
    jsonp原理详解
    垂直居中
    window.moveTo(),window.moveBy()不生效
  • 原文地址:https://www.cnblogs.com/denggelin/p/5656569.html
Copyright © 2011-2022 走看看