【发布时间】:2015-08-26 07:19:12
【问题描述】:
对于以下表示矩阵的函数
class m // matrix
{
private:
double **matrix;
int nrows, ncols;
class p
{
private:
double *arr;
public:
p (double *a)
: arr (a)
{
}
double &operator[] (int c)
{
return arr[c];
}
};
public:
m (int nrows, int ncols)
{
this->matrix = new double *[nrows];
for (int i = 0; i < nrows; ++i)
{
this->matrix[i] = new double [ncols];
}
this->nrows = nrows;
this->ncols = ncols;
}
~m()
{
for (int i = 0; i < this->nrows; ++i)
{
delete [] this->matrix[i];
}
delete this->matrix;
}
void assign (int r, int c, double v)
{
this->matrix[r][c] = v;
}
p operator[] (int r)
{
return p (this->matrix[r]);
}
};
操作符适用于元素访问,但不适用于元素更改。如何将assign()函数的功能添加到操作符中?
【问题讨论】:
-
不是另一个锯齿状边缘矩阵。请使用连续的内存块。
-
怎么不工作? This works fine.