sort_unique.cpp 946 B

12345678910111213141516171819202122232425262728293031323334
  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. ///[sort_unique]
  13. // Remove all non-unique elements from a container.
  14. #include <iostream>
  15. #include <vector>
  16. #include <range/v3/action/sort.hpp>
  17. #include <range/v3/action/unique.hpp>
  18. #include <range/v3/view/all.hpp>
  19. using std::cout;
  20. int main()
  21. {
  22. std::vector<int> vi{9, 4, 5, 2, 9, 1, 0, 2, 6, 7, 4, 5, 6, 5, 9, 2, 7,
  23. 1, 4, 5, 3, 8, 5, 0, 2, 9, 3, 7, 5, 7, 5, 5, 6, 1,
  24. 4, 3, 1, 8, 4, 0, 7, 8, 8, 2, 6, 5, 3, 4, 5};
  25. using namespace ranges;
  26. vi |= actions::sort | actions::unique;
  27. // prints: [0,1,2,3,4,5,6,7,8,9]
  28. cout << views::all(vi) << '\n';
  29. }
  30. ///[sort_unique]