accumulate_ints.cpp 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. // Range v3 library
  2. //
  3. // Copyright Eric Niebler 2019
  4. //
  5. // Use, modification and distribution is subject to the
  6. // Boost Software License, Version 1.0. (See accompanying
  7. // file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. // Project home: https://github.com/ericniebler/range-v3
  11. //
  12. ///[accumulate_ints]
  13. // Sums the first ten squares and prints them, using views::ints to generate
  14. // and infinite range of integers, views::transform to square them, views::take
  15. // to drop all but the first 10, and accumulate to sum them.
  16. #include <iostream>
  17. #include <vector>
  18. #include <range/v3/numeric/accumulate.hpp>
  19. #include <range/v3/view/iota.hpp>
  20. #include <range/v3/view/take.hpp>
  21. #include <range/v3/view/transform.hpp>
  22. using std::cout;
  23. int main()
  24. {
  25. using namespace ranges;
  26. int sum = accumulate(views::ints(1, unreachable) | views::transform([](int i) {
  27. return i * i;
  28. }) | views::take(10),
  29. 0);
  30. // prints: 385
  31. cout << sum << '\n';
  32. }
  33. ///[accumulate_ints]