comprehension_conversion.cpp 999 B

1234567891011121314151617181920212223242526272829303132333435
  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. ///[comprehension_conversion]
  13. // Use a range comprehension (views::for_each) to construct a custom range, and
  14. // then convert it to a std::vector.
  15. #include <iostream>
  16. #include <vector>
  17. #include <range/v3/range/conversion.hpp>
  18. #include <range/v3/view/for_each.hpp>
  19. #include <range/v3/view/iota.hpp>
  20. #include <range/v3/view/repeat_n.hpp>
  21. using std::cout;
  22. int main()
  23. {
  24. using namespace ranges;
  25. auto vi = views::for_each(views::ints(1, 6),
  26. [](int i) { return yield_from(views::repeat_n(i, i)); }) |
  27. to<std::vector>();
  28. // prints: [1,2,2,3,3,3,4,4,4,4,5,5,5,5,5]
  29. cout << views::all(vi) << '\n';
  30. }
  31. ///[comprehension_conversion]