C++ Wiki

2015.02.25

Tags: C++11

C++11のラムダ関数

サンプルコード

#include <iostream>
#include <functional>

int main()
{
  std::function<int(int)> func = [&](int n){ return (n == 0) ? 1 : (func(n - 1) * n); };

  for(int i = 1; i <= 5; ++i){
    std::cout << i << "! = " << func(i) << std::endl;
  }

  return 0;
}

実行結果

1! = 1
2! = 2
3! = 6
4! = 24
5! = 120