⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch7.7fibonacci.cc

📁 C++ source code for book-C++ and Object Oriented Numeric computing for scientists and engineers
💻 CC
字号:
// template metaprogram for Fibonacci numbers

#include <iostream>

template<int N> struct fib{
  enum { value = fib<N-1>::value + fib<N-2>::value};
};

template<> struct fib<1>{
  enum { value = 1 };
};

template<> struct fib<0>{
  enum { value = 0 };
};

// we may also define static constants for value
template<int N> struct fib2 {
  static const int value = fib2<N-1>::value + fib2<N-2>::value;
};

template<> struct fib2<1> {
  static const int value = 1;
};

template<> struct fib2<0> {
  static const int value = 0;
};


int main() {
  const int f3 = fib<3>::value;
  const int f9 = fib<9>::value;
  const int b9 = fib2<9>::value;

  std::cout << " f3 = " << f3 << '\n';
  std::cout << " f9 = " << f9 << '\n';
  std::cout << " b9 = " << b9 << '\n';
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -