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
|
#include <iostream>
#include <vector>
using namespace std;
using matrix = vector<vector<int>>;
//======================================================================
void print( const matrix &M )
{
for ( auto &row : M )
{
for ( auto e : row ) cout << e << '\t';
cout << '\n';
}
}
//======================================================================
matrix rowSlice( const matrix &original, const vector<int> &rowIndices )
{
matrix result;
for ( int i : rowIndices ) result.push_back( original[i] );
return result;
}
//======================================================================
vector<int> getIndices( const vector<int> &F, int value )
{
vector<int> result;
for ( int i = 0; i < F.size(); i++ ) if ( F[i] == value ) result.push_back( i );
return result;
}
//======================================================================
int main()
{
matrix M = { { 3, 6, 7 }, { 1, 4, 7 }, { 4, 6, 8 }, { 1, 4, 5 } };
vector<int> F = { 1, 2, 1, 2 };
matrix M1 = rowSlice( M, getIndices( F, 1 ) );
matrix M2 = rowSlice( M, getIndices( F, 2 ) );
cout << "M1:\n"; print( M1 ); cout << "\n\n";
cout << "M2:\n"; print( M2 ); cout << "\n\n";
}
| |