fork(1) download
  1. #include <cstddef>
  2. #include <algorithm>
  3. #include <iterator>
  4. #include <iostream>
  5.  
  6. template<typename T, ::std::size_t N>
  7. static ::std::ostream& print_array(::std::ostream& out, T(&arr)[N]) {
  8. out << "[" << arr[0];
  9. for(::std::size_t i = 1; i < N; ++i) {
  10. out << ", " << arr[i];
  11. }
  12. out << "]";
  13. return out;
  14. }
  15.  
  16. int main() {
  17. int array[] = {1, 3, 2, 4};
  18. auto begin = ::std::begin(array);
  19. auto end = ::std::end(array);
  20.  
  21. ::std::sort(begin, end);
  22. print_array(::std::cout, array) << "\n";
  23.  
  24. ::std::sort(begin, end, [](int lhs, int rhs) { return lhs > rhs; });
  25. print_array(::std::cout, array) << "\n";
  26.  
  27. ::std::sort(begin, end, [](int lhs, int rhs) {
  28. if(lhs%2 != rhs%2) return lhs%2 < rhs%2;
  29. else return lhs > rhs;
  30. });
  31. print_array(::std::cout, array) << "\n";
  32. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
[1, 2, 3, 4]
[4, 3, 2, 1]
[4, 2, 3, 1]