我想知道当使用矢量矢量来表示2D矩阵或者创建类似的类时(任何类型)有什么区别:
template < class T >
class Matrix2D {
public:
Matrix2D( unsigned m, unsigned n ) : m( m ), n( n ), x( m * n ) {} ;
Matrix2D( const Matrix2D<T> &matrix ) : m( matrix.m ), n( matrix.n) x( matrix.x ) {} ;
Matrix2D& operator= ( const Matrix2D<T> &matrix ) ;
T& operator ()( unsigned i, unsigned j ) ;
void resize( int nx, int ny ) ;
private:
unsigned m, n ;
std::vector< T > x ;
} ;
template <class T>
T& Matrix2D<T>::operator ()( unsigned i, unsigned j ) {
return x[ j + n * i ] ;
}
template <class T>
Matrix2D<T>& Matrix2D<T>::operator= ( const Matrix2D<T> &matrix ) {
m = matrix.m ;
n = matrix.n ;
x = matrix.x ;
return *this ;
}
template <class T>
void Matrix2D<T>::resize( int nx, int ny ) {
m = nx ;
n = ny ;
x.resize( nx * ny ) ;
}
编辑:忽略调整大小方法,因为Erik指出它不会保留原始数据。我只为一个我不介意的具体任务添加了。基本类只是ctor和()运算符。
答案 0 :(得分:2)
.resize()
不会将现有数据保留在原始位置。operator()
与operator[]
std::
算法简而言之,该课程很好,可能更适合专业用途,但在通用目的方面表现不佳。