fork(1) download
  1. #include <thread>
  2. #include <vector>
  3. #include <functional>
  4. #include <iostream>
  5.  
  6. #include <cassert>
  7.  
  8. template <typename Iter>
  9. void rev(Iter from, Iter mid, Iter to)
  10. {
  11. const auto len = mid - from;
  12.  
  13. if (len == 0)
  14. {
  15. return;
  16. }
  17. else if (len == 1)
  18. {
  19. std::iter_swap(from, to - 1);
  20. return;
  21. }
  22.  
  23. auto left = from + ( len / 2 );
  24. auto right = to - ( len / 2 );
  25.  
  26. auto t1 = std::thread{ [=]() { rev(from, left, to); } };
  27. auto t2 = std::thread{ [=]() { rev(left, mid, right); } };
  28.  
  29. t1.join();
  30. t2.join();
  31. }
  32.  
  33. template <typename Iter>
  34. void rev(Iter begin, Iter end)
  35. {
  36. const auto len = end - begin;
  37. rev(begin, begin + (len / 2), end);
  38. }
  39.  
  40. int main()
  41. {
  42. std::vector<int> v = { 1,2,3,4,5,6,7,8,9 };
  43. rev( v.begin(), v.end() );
  44. assert(v == (std::vector<int>{ 9,8,7,6,5,4,3,2,1 }));
  45.  
  46. std::cout << "ok\n";
  47. }
Success #stdin #stdout 0s 158592KB
stdin
Standard input is empty
stdout
ok