fork download
#include <iostream>
#include <iomanip>
#include <algorithm>
 
 const unsigned lineWidth = 80 ;
 const unsigned columnWidth = 10 ;
 const unsigned columns = lineWidth / columnWidth ;
 
void print( const int* t, unsigned size )
{ 
    for ( unsigned i=0; i<size; ++i )
    {
        std::cout << std::setw(columnWidth) << t[i] ;
 
        if ( i > columns )
            std::cout << '\n' ;
    }
 
    std::cout << '\n' ;
}
 
void print( const int* t, const unsigned* indices, unsigned size )
{ 
    for ( unsigned i=0; i<size; ++i )
    {
        std::cout << std::setw(columnWidth) << t[indices[i]] ;
 
        if ( i > columns )
            std::cout << '\n' ;
    }
 
    std::cout << '\n' ;
}
 
int main()
{
    const unsigned sz = 10 ;
    int arr[sz] = {1, 5, 22, 14, 6, -5, 7, 9, 12, 15 } ;
    unsigned arr_index[sz] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } ;
 
    print(arr, sz) ;
    print(arr, arr_index, sz) ;
    std::cout << '\n' ;
 
    std::sort(std::begin(arr_index), std::end(arr_index), [&](unsigned i, unsigned j){ return arr[i] < arr[j]; }) ;
    print(arr, sz) ;
    print(arr, arr_index, sz) ;
    std::cout << '\n' ;
 
    std::sort(std::begin(arr_index), std::end(arr_index), [&](unsigned i, unsigned j){ return arr[j] < arr[i]; }) ;
    print(arr,sz) ;
    print(arr, arr_index, sz) ;
}
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