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 }
  • 相关阅读:
    HDU 5090 Game with Pearls
    HDU 1394 Minimum Inversion Number
    HDU 1698 Just a Hook
    POJ 2104 K-th Number
    UVA 1160
    HDU 5895 Mathematician QSC
    HDU 3294 Girls' research
    HDU 3068 最长回文
    PyCharm每日技巧-1
    如何一年考过日语一级
  • 原文地址:https://www.cnblogs.com/denggelin/p/5656569.html
Copyright © 2011-2022 走看看