fork download
  1. #include <cstddef>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <string>
  5.  
  6. template <typename iter>
  7. void print(iter begin, iter end, const std::string& separator = " ", const std::string& terminator = "\n", std::ostream& os = std::cout)
  8. {
  9. while (begin != end)
  10. {
  11. os << *begin++ ;
  12.  
  13. if (begin != end)
  14. os << separator;
  15. }
  16.  
  17. os << terminator;
  18. }
  19.  
  20. void myreverse(int* beg, int* end)
  21. {
  22. using std::swap;
  23.  
  24. if (beg && end && beg < --end)
  25. while (beg < end)
  26. swap(*beg++, *end--);
  27. }
  28.  
  29. int main()
  30. {
  31. const std::size_t size = 5;
  32. int arr[size] = { 1, 2, 3, 4, 5 };
  33.  
  34. int* arr_beg = arr;
  35. int* arr_end = arr + size;
  36.  
  37. print(arr_beg, arr_end, ", ");
  38.  
  39. std::reverse(arr_beg, arr_end);
  40. print(arr_beg, arr_end, ", ");
  41.  
  42. myreverse(arr_beg, arr_end);
  43. print(arr_beg, arr_end, ", ");
  44. }
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
1, 2, 3, 4, 5
5, 4, 3, 2, 1
1, 2, 3, 4, 5