fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <iterator>
  4. using namespace std;
  5.  
  6.  
  7. void reverse(int * from, int * to) {
  8. while ((to - from) > 1) {
  9. --to;
  10. int temp = *from;
  11. *from = *to;
  12. *to = temp;
  13. ++from;
  14. }
  15. }
  16.  
  17. int const * find(int const * from,
  18. int const * const to,
  19. int const value) {
  20. while ((from != to) && (*from != value)) {
  21. ++from;
  22. }
  23. return from;
  24. }
  25.  
  26. void reverse_until (int * const from,
  27. int * const to,
  28. int const sentinel) {
  29. int const * const position_sentinel = find(from, to, sentinel);
  30. reverse(from, from + (position_sentinel - from));
  31. }
  32.  
  33.  
  34. int main() {
  35. int test[10];
  36. for (size_t i = 0; i < 10; ++i) {
  37. test [i] = i + 1;
  38. }
  39. reverse_until (test, test + 10, 6);
  40. copy(test, test + 10, ostream_iterator<int>{cout, " "});
  41. return 0;
  42. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
5 4 3 2 1 6 7 8 9 10