power.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Copyright Oliver Kowalke 2009.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #include <boost/coroutine/all.hpp>
  6. #include <cstdlib>
  7. #include <iostream>
  8. #include <boost/bind.hpp>
  9. #include <boost/foreach.hpp>
  10. #include <boost/range.hpp>
  11. void power( boost::coroutines::asymmetric_coroutine< int >::push_type & sink, int number, int exponent)
  12. {
  13. int counter = 0;
  14. int result = 1;
  15. while ( counter++ < exponent)
  16. {
  17. result = result * number;
  18. sink( result);
  19. }
  20. }
  21. int main()
  22. {
  23. {
  24. std::cout << "using range functions" << std::endl;
  25. boost::coroutines::asymmetric_coroutine< int >::pull_type source( boost::bind( power, _1, 2, 8) );
  26. boost::coroutines::asymmetric_coroutine< int >::pull_type::iterator e( boost::end( source) );
  27. for ( boost::coroutines::asymmetric_coroutine< int >::pull_type::iterator i( boost::begin( source) );
  28. i != e; ++i)
  29. std::cout << * i << " ";
  30. }
  31. {
  32. std::cout << "\nusing BOOST_FOREACH" << std::endl;
  33. boost::coroutines::asymmetric_coroutine< int >::pull_type source( boost::bind( power, _1, 2, 8) );
  34. BOOST_FOREACH( int i, source)
  35. { std::cout << i << " "; }
  36. }
  37. std::cout << "\nDone" << std::endl;
  38. return EXIT_SUCCESS;
  39. }