conversion.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Range v3 library
  2. //
  3. // Copyright Eric Niebler 2014-present
  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. #include <utility> // for std::swap on C++14.
  12. #include <map>
  13. #include <set>
  14. #include <list>
  15. #include <sstream>
  16. #include <string>
  17. #include <vector>
  18. #include <range/v3/core.hpp>
  19. #include <range/v3/view/any_view.hpp>
  20. #include <range/v3/view/concat.hpp>
  21. #include <range/v3/view/drop.hpp>
  22. #include <range/v3/view/take.hpp>
  23. #include <range/v3/view/repeat.hpp>
  24. #include <range/v3/view/reverse.hpp>
  25. #include <range/v3/view/transform.hpp>
  26. #include <range/v3/view/for_each.hpp>
  27. #include <range/v3/view/iota.hpp>
  28. #include <range/v3/view/zip.hpp>
  29. #include "../simple_test.hpp"
  30. #include "../test_utils.hpp"
  31. int main()
  32. {
  33. using namespace ranges;
  34. // 1-d vector
  35. auto v = views::ints | views::take(10) | to<std::vector>();
  36. ::check_equal(v, {0,1,2,3,4,5,6,7,8,9});
  37. v = views::iota(10) | views::take(10) | views::reverse | to<std::vector>();
  38. ::check_equal(v, {19,18,17,16,15,14,13,12,11,10});
  39. // 1-d list
  40. auto l = views::ints | views::take(10) | to<std::list>();
  41. ::check_equal(l, {0,1,2,3,4,5,6,7,8,9});
  42. l = views::iota(10) | views::take(10) | views::reverse | to<std::list>();
  43. ::check_equal(l, {19,18,17,16,15,14,13,12,11,10});
  44. // 2-d vector
  45. auto vv = views::repeat_n(views::ints(0, 8), 10) | to<std::vector<std::vector<int>>>();
  46. ::check_equal(vv, std::vector<std::vector<int>>(10, {0,1,2,3,4,5,6,7}));
  47. // issue #556
  48. {
  49. std::string s{"abc"};
  50. any_view<any_view<char, category::random_access>, category::random_access> v1 =
  51. views::single(s | views::drop(1));
  52. any_view<any_view<char, category::random_access>, category::random_access> v2 =
  53. views::single(s | views::drop(2));
  54. auto v3 = views::concat(v1, v2);
  55. auto owner1 = v3 | to<std::vector<std::vector<char>>>();
  56. auto owner2 = v3 | to<std::vector<std::string>>();
  57. ::check_equal(owner1, std::vector<std::vector<char>>{{'b', 'c'}, {'c'}});
  58. ::check_equal(owner2, std::vector<std::string>{{"bc"}, {"c"}});
  59. }
  60. // map
  61. auto to_string = [](int i){ std::stringstream str; str << i; return str.str(); };
  62. auto m = views::zip(views::ints, views::ints | views::transform(to_string)) |
  63. views::take(5) | to<std::map<int, std::string>>();
  64. using P = std::pair<int const, std::string>;
  65. ::check_equal(m, {P{0,"0"}, P{1,"1"}, P{2,"2"}, P{3,"3"}, P{4,"4"}});
  66. // Another way to say the same thing, but with a range comprehension:
  67. m = views::for_each(views::ints(0,5), [&](int i) {
  68. return yield(std::make_pair(i, to_string(i)));
  69. }) | to<std::map<int, std::string>>();
  70. ::check_equal(m, {P{0,"0"}, P{1,"1"}, P{2,"2"}, P{3,"3"}, P{4,"4"}});
  71. // set
  72. CPP_assert(range<std::set<int>>);
  73. CPP_assert(!view_<std::set<int>>);
  74. auto s = views::ints | views::take(10) | to<std::set<int>>();
  75. ::check_equal(s, {0,1,2,3,4,5,6,7,8,9});
  76. static_assert(!view_<std::initializer_list<int>>, "");
  77. return ::test_result();
  78. }