Boost.Coroutine でフィボナッチ
リハビリがてら軽く書いてみました。
[ソース]
#include <iostream> #include <boost/coroutine/all.hpp> template<typename C> void fibonacci(C& c){ typedef typename C::arg_type value_type; value_type first = 0; c(first); value_type second = 1; c(second); while( 1 ){ value_type third = first + second; first = second; second = third; c(third); } } int main(){ typedef boost::coroutines::coroutine<int()> coroutine; coroutine fib(fibonacci); coroutine::iterator it = boost::begin(fib); for(int i = 0 ; i < 15 ; ++i){ std::cout << *it << std::endl; ++it; } return 0; }
[出力]
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
[boost]
- ver 1.53.0