大佬教程收集整理的这篇文章主要介绍了c – 为什么在创建矩阵类时使用向量是不好的?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
template<typename T,std::uint32_t Height,std::uint32_t Width> class Matrix { private: std::array<std::array<T,Width>,Height> Elements; static_assert(std::is_arithmetic<T>::value,"Argument T must be of arithmetic type."); public: Matrix(); Matrix(T* Data); Matrix(T** Data); Matrix(T Data[Height][Width]); Matrix(const std::array<std::array<T,Height> &Data); inline int size() {return Width * Height;} inline const int size() const {return Width * Height;} inline int width() {return Width;} inline const int width() const {return Width;} inline int height() {return Height;} inline const int height() const {return Height;} std::array<T,Width>& operator[](int Index); const std::array<T,Width>& operator[](int Index) const; Matrix& operator = (const Matrix &M); Matrix& operator + (const Matrix &M); Matrix& operator - (const Matrix &M); Matrix& operator * (const Matrix &M); }; template<typename T,std::uint32_t Width> Matrix<T,Height,Width>::Matrix() {std::memset(&Elements,sizeof(T) * Width * Height);} template<typename T,Width>::Matrix(T* Data) {if (Data) std::memcpy(&Elements,Data,Width>::Matrix(T** Data) {if (Data) std::memcpy(&Elements,&Data[0][0],Width>::Matrix(T Data[Height][Width]) {std::memcpy(&Elements,Width>::Matrix(const std::array<std::array<T,Height> &Data) {std::memcpy(&Elements,std::uint32_t Width> std::array<T,Width>& Matrix<T,Width>::operator[](int Index) {return Elements[Index];} template<typename T,std::uint32_t Width> const std::array<T,Width>::operator[](int Index) const {return Elements[Index];}
因为我在线阅读并且许多评论说不使用向量而是使用数组或使用std :: valarray ..
现在我问的原因是因为我想重写我的矩阵类,所以我不必继续这样做:Matrix< Type,Width,Height>每次..我宁愿在构造函数中执行一次,也不必为每个函数键入它.如上所述.例如,我必须为每个Matrix参数写出每个函数AND的长模板声明.另外我不知道如何删除向量的resize / pushback,这样当用户索引向量时,它们将无法调整大小,因此我使用了数组.
我打算使用一维数组并将其编入索引(I *宽度J),但后来我丢失了[] []运算符.
使用矢量矢量是不是很糟糕?有什么想法来提高我的课程并使其符合RAII标准吗?我不太明白如何使用valarray,上面的内容很难维护.任何想法都表示赞赏.
以上是大佬教程为你收集整理的c – 为什么在创建矩阵类时使用向量是不好的?全部内容,希望文章能够帮你解决c – 为什么在创建矩阵类时使用向量是不好的?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。