fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <algorithm>
  4.  
  5. const unsigned lineWidth = 80 ;
  6. const unsigned columnWidth = 10 ;
  7. const unsigned columns = lineWidth / columnWidth ;
  8.  
  9. void print( const int* t, unsigned size )
  10. {
  11. for ( unsigned i=0; i<size; ++i )
  12. {
  13. std::cout << std::setw(columnWidth) << t[i] ;
  14.  
  15. if ( i > columns )
  16. std::cout << '\n' ;
  17. }
  18.  
  19. std::cout << '\n' ;
  20. }
  21.  
  22. void print( const int* t, const unsigned* indices, unsigned size )
  23. {
  24. for ( unsigned i=0; i<size; ++i )
  25. {
  26. std::cout << std::setw(columnWidth) << t[indices[i]] ;
  27.  
  28. if ( i > columns )
  29. std::cout << '\n' ;
  30. }
  31.  
  32. std::cout << '\n' ;
  33. }
  34.  
  35. int main()
  36. {
  37. const unsigned sz = 10 ;
  38. int arr[sz] = {1, 5, 22, 14, 6, -5, 7, 9, 12, 15 } ;
  39. unsigned arr_index[sz] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } ;
  40.  
  41. print(arr, sz) ;
  42. print(arr, arr_index, sz) ;
  43. std::cout << '\n' ;
  44.  
  45. std::sort(std::begin(arr_index), std::end(arr_index), [&](unsigned i, unsigned j){ return arr[i] < arr[j]; }) ;
  46. print(arr, sz) ;
  47. print(arr, arr_index, sz) ;
  48. std::cout << '\n' ;
  49.  
  50. std::sort(std::begin(arr_index), std::end(arr_index), [&](unsigned i, unsigned j){ return arr[j] < arr[i]; }) ;
  51. print(arr,sz) ;
  52. print(arr, arr_index, sz) ;
  53. }
Success #stdin #stdout 0s 2856KB
stdin
Standard input is empty
stdout
         1         5        22        14         6        -5         7         9        12        15

         1         5        22        14         6        -5         7         9        12        15


         1         5        22        14         6        -5         7         9        12        15

        -5         1         5         6         7         9        12        14        15        22


         1         5        22        14         6        -5         7         9        12        15

        22        15        14        12         9         7         6         5         1        -5