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
|
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using Row = vector<double>;
using Matrix = vector<Row>;
//======================================================================
bool operator < ( const Row &a, const Row &b ) // Define ordering for rows
{
for ( int i = 0; i < a.size(); i++ )
{
if ( a[i] < b[i] ) return true;
if ( a[i] > b[i] ) return false;
}
return false;
}
//======================================================================
ostream &operator << ( ostream &strm, const Matrix &M )
{
for ( auto &row : M )
{
for ( auto e : row ) strm << e << '\t';
strm << '\n';
}
return strm;
}
//======================================================================
int main()
{
Matrix M{ { 6, 1, 1, 2 }, { 7, 2, 9, 4 }, { 7, 3, 1, 5 }, { 7, 2, 9, 3 } };
cout << "Pre-sorting:\n" << M << '\n';
sort( M.begin(), M.end() ); // Uses defined < operator
cout << "Post-sorting:\n" << M << '\n';
}
//======================================================================
|