unique.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. #include <random>
  10. #include <vector>
  11. #include <range/v3/core.hpp>
  12. #include <range/v3/view/iota.hpp>
  13. #include <range/v3/view/repeat_n.hpp>
  14. #include <range/v3/view/for_each.hpp>
  15. #include <range/v3/view/take.hpp>
  16. #include <range/v3/algorithm/shuffle.hpp>
  17. #include <range/v3/algorithm/equal.hpp>
  18. #include <range/v3/algorithm/is_sorted.hpp>
  19. #include <range/v3/action/shuffle.hpp>
  20. #include <range/v3/action/sort.hpp>
  21. #include <range/v3/action/unique.hpp>
  22. #include "../simple_test.hpp"
  23. #include "../test_utils.hpp"
  24. int main()
  25. {
  26. using namespace ranges;
  27. std::mt19937 gen;
  28. // [1,2,2,3,3,3,4,4,4,4,5,5,5,5,5,...]
  29. auto v =
  30. views::for_each(views::ints(1,100), [](int i){
  31. return yield_from(views::repeat_n(i,i));
  32. }) | to<std::vector>();
  33. check_equal(views::take(v, 15), {1,2,2,3,3,3,4,4,4,4,5,5,5,5,5});
  34. v |= actions::shuffle(gen);
  35. CHECK(!is_sorted(v));
  36. v |= actions::sort | actions::unique;
  37. CHECK(equal(v, views::ints(1,100)));
  38. return ::test_result();
  39. }