programming-examples/c++/Others/matrix.h - Simple matrix class.cpp

33 lines
917 B
C++
Raw Normal View History

2019-11-15 12:59:38 +01:00
matrix.h - Simple matrix class
#ifndef MATRIX_H
#define MATRIX_H
#include "vector.h"
template <class Object>
class matrix
{
public:
matrix( int rows, int cols ) : array( rows )
{
for( int i = 0; i < rows; i++ )
array[ i ].resize( cols );
}
matrix( const matrix & rhs ) : array( rhs.array ) { }
const vector<Object> & operator[]( int row ) const
{ return array[ row ]; }
vector<Object> & operator[]( int row )
{ return array[ row ]; }
int numrows( ) const
{ return array.size( ); }
int numcols( ) const
{ return numrows( ) ? array[ 0 ].size( ) : 0; }
private:
vector< vector<Object> > array;
};
#endif