1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <cassert>
using namespace std;
//======================================================================
int main()
{
vector<int> quality = { 1, 7, 2 };
vector< vector<int> > A = { { 1, 2, 3 },
{ 4, 7, 9 },
{ 1, 3, 9 } };
int rows = A.size();
int cols = A[0].size(); assert( quality.size() == cols );
// Pack quality and original columns into a vector of pairs
vector< pair<int,int> > Q( cols );
for ( int c = 0; c < cols; c++ )
{
Q[c].first = quality[c];
Q[c].second = c;
}
// Sort on the basis of quality
sort( Q.begin(), Q.end(), []( const pair<int,int> &a, const pair<int,int> &b ){ return a.first < b.first; } );
// Permute columns in original quality and array
vector< vector<int> > temp( rows, vector<int>( cols ) );
for ( int c = 0; c < cols; c++ )
{
quality[c] = Q[c].first;
int oldcol = Q[c].second;
for ( int r = 0; r < rows; r++ ) temp[r][c] = A[r][oldcol];
}
A = temp;
// Output
cout << "Quality:\n";
for ( int i : quality ) cout << setw( 2 ) << i << " ";
cout << "\n\n";
cout << "Data:\n";
for ( const auto &row : A )
{
for ( int i : row ) cout << setw( 2 ) << i << " ";
cout << '\n';
}
}
//======================================================================
| |