C ++矩阵类

编程入门 行业动态 更新时间:2024-10-08 04:35:05
本文介绍了C ++矩阵类 - 建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图在C ++中构建一个模板化的Matrix类。下面是它的实现。 我实现了两个运算符+,+ =现在只是为了给出一个关于它的外观的想法,我认为最好是在继续进一步之前请求反馈。

I am trying to build a templated Matrix class in C++. Below is its implementation. I implemented two operators +,+= as of now just to give an idea on how it will look and I thought it would be best to ask for feedback before I proceed any further.

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

The whole implementation is public and also there are no explicit bound/error checks, this is because its not meant to be a full fledged matrix library and hence avoided unnecessary code.

如果有人对此发表评论,将会非常有帮助,并且可能会建议一些改进或建议。

It would be very helpful if someone could comment on it and may be suggest a few improvements or suggestions.

谢谢。

Thank you.

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; }

编辑:谢谢大家的建议。

Thank you all for the suggestions.

我只有一个小问题,说我想实现错误检查(例如:检查边界,有效的参数等),但我想要提供用户有一个选项,完全禁用错误检查,有没有什么好的方法来实现这个? 我需要的是像例子:`ios_base :: sync_with_stdio(0);. 再次感谢。

I just have one small question, say I do want to implement error checking (ex: checking for bounds,valid arguments etc..) however I do want the provide user with an option to disable error checking completely, is there any good way to implement this ? What I need is something like example:`ios_base::sync_with_stdio(0);. Thanks again.

推荐答案

以下几点:

  • 使用单个 std :: vector
  • 使您的数据成员为私有,以保护矩阵不受无意的大小更改。例如,用户代码当前可以调整向量的大小,但保留旧的 r 和 c 。
  • 重载 operator()来访问矩阵(const和非const)。如果你真的必须使用 matrix [r] [c] 语法而不是 matrix(r,c)重载运算符[] 并返回一个迭代器到正确的行(向量迭代器是随机访问,所以他们会提供 operator [] $ b
  • $ b
  • 使用初始化列表作为其他人建议。
  • 让当前采用 T * 的构造函数使用迭代器。这样,您可以自动获得指针支持以及许多其他酷的东西,例如调试迭代器的范围检查,兼容值类型的自动类型转换和对所有其他随机访问迭代器的支持。也请考虑逐行填充矩阵,以便您也可以使用向前的迭代器。
  • Use a single std::vector<T> instead of std::vector<std::vector<T>>. Index it with y*r+x - use and operator overload to make this easier (see next point). This will be more memory efficient and slightly faster (and your init will be a lot easier: resize(r*c)).
  • Make your data members private to protect your matrix against unintentional size changes. For example, user code can currently resize the vectors but leave the old r and c.
  • Overload operator() to access the matrix (both const and non-const). If you really must use the matrix[r][c] syntax instead of matrix(r,c), consider overloading operator[] and returning an iterator to the correct row (vector iterators are random access, so they will provide operator[]).
  • Implement operator+ as a non-friend non-member function instead - Improves encapsulation!
  • Use initialization lists as others suggested.
  • Let the constructor that currently takes a T* take an iterator instead. That way you automagically get the pointers support along with a lot of other cool things, such as range checking for debug-iterators, automatic type conversion for compatible value types and support for all other random access iterators. Also consider filling your matrix row-wise, so you can use forward iterators too.

更多推荐

C ++矩阵类

本文发布于:2023-10-23 02:39:43,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:矩阵

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!