fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <chrono>
  5.  
  6. using namespace std;
  7.  
  8. template <class F>
  9. chrono::milliseconds take_time(F&& f)
  10. {
  11. auto const begin = chrono::system_clock::now();
  12. f();
  13. auto const end = chrono::system_clock::now();
  14. return chrono::duration_cast<chrono::milliseconds>( end - begin );
  15. }
  16.  
  17. int main()
  18. {
  19. vector< vector<int> > arr {
  20. {1,5,3,4},
  21. {0,4,1,0},
  22. {7,2,3,1}
  23. };
  24. auto const time = take_time([&] {
  25. vector<int> tmp;
  26.  
  27. for (auto& r : arr)
  28. copy( r.begin(), r.end(), back_inserter( tmp ) );
  29.  
  30. sort( tmp.begin(), tmp.end() );
  31.  
  32. for (unsigned r=0; r<arr.size(); ++r) {
  33. for (unsigned c=0; c<arr[r].size(); ++c) {
  34. const unsigned i= r * arr[r].size() + c;
  35. arr[r][c] = tmp[i];
  36. }
  37. }
  38. });
  39. /* results */
  40. for (auto& row : arr)
  41. {
  42. for (auto& col : row)
  43. cout << col << ", ";
  44. cout << '\n';
  45. }
  46. cout << time.count() << " ms." << endl;
  47.  
  48. return 0;
  49. }
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
0, 0, 1, 1, 
1, 2, 3, 3, 
4, 4, 5, 7, 
0 ms.