gpt4 book ai didi

C++ Matrix 类 - 建议

转载 作者:太空狗 更新时间:2023-10-29 23:37:39 26 4
gpt4 key购买 nike

我正在尝试用 C++ 构建一个模板化的 Matrix 类。下面是它的实现。到目前为止,我实现了两个运算符 +,+= 只是为了了解它的外观,我认为最好在继续进行之前征求反馈。

整个实现是公开的,也没有明确的绑定(bind)/错误检查,这是因为它不是一个完整的矩阵库,因此避免了不必要的代码。

如果有人可以对此发表评论并提出一些改进或建议,那将非常有帮助。

谢谢。

template<class T>
class Matrix
{
public:
int r,c;
vector< vector< T > >mat;

Matrix() {}

// Constructor to set the size of the matrix
Matrix(int _r,int _c)
{
r=_r;c=_c;
mat.resize(r);
for(int i=0;i<r;i++)
mat[i].resize(c);
}
// Constructor to build a matrix from a C 2d array
// Pointer to the first element is passed (&arr[0][0])
Matrix(T *arr,int _r,int _c)
{
r=_r;c=_c;
mat.resize(r);
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
mat[i].push_back(arr[i*c+j]);
}
template<typename U>
Matrix<T>& operator +=(const Matrix<U>&M)
{
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
mat[i][j]+=static_cast<T>(M.mat[i][j]);
return *this;
}
template<typename U>
Matrix<T> operator +(const Matrix<U>&M)
{
Matrix<T>tmp=*this;
return tmp+=M;
}
};

template<typename T>
istream& operator >>(istream &in,Matrix<T>&M)
{
in>>M.r>>M.c;
Matrix<T>tmp(M.r,M.c);
for(int i=0;i<M.r;i++)
for(int j=0;j<M.c;j++)
in>>tmp.mat[i][j];
M=tmp;
return in;
}
template<typename T>
ostream& operator <<(ostream &out,Matrix<T>M)
{
for(int i=0;i<M.r;i++)
{
for(int j=0;j<M.c;j++)
cout<<M.mat[i][j]<<" ";
cout<<endl;
}
return out;
}

编辑:谢谢大家的建议。

我只有一个小问题,说我确实想实现错误检查(例如:检查边界、有效参数等)但是我确实希望为用户提供一个选项来完全禁用错误检查,有没有实现这个的好方法?我需要的是类似 example:`ios_base::sync_with_stdio(0); 的东西。再次感谢。

最佳答案

几点:

  • 使用单个 std::vector<T>而不是 std::vector<std::vector<T>> .用 y*r+x 对其进行索引——使用 and 运算符重载使这更容易(见下一点)。这将提高内存效率并稍微加快(并且您的初始化会容易得多:resize(r*c))。
  • 将您的数据成员设为私有(private),以保护您的矩阵免受无意的大小更改。例如,用户代码当前可以调整 vector 的大小但保留旧的 rc .
  • 重载operator()访问矩阵(const 和非常量)。如果你真的必须使用 matrix[r][c]语法而不是 matrix(r,c) , 考虑重载 operator[]并将迭代器返回到正确的行( vector 迭代器是随机访问的,因此它们将提供 operator[] )。
  • 实现 operator+作为非友元非成员函数 - 改进封装!
  • 按照其他人的建议使用初始化列表。
  • 让当前接受 T* 的构造函数取而代之的是迭代器。通过这种方式,您可以自动获得指针支持以及许多其他很酷的东西,例如调试迭代器的范围检查、兼容值类型的自动类型转换以及对所有其他随机访问迭代器的支持。还可以考虑按行填充矩阵,这样您也可以使用前向迭代器。

关于C++ Matrix 类 - 建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5633609/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com