一个使用 2D 动态数组声明的模板化矩阵类,包含矩阵最需要的数学函数以及标准 C++ 中所需的所有运算符(不使用任何 vc 命令)






1.30/5 (10投票s)
此程序由 Shahin Namini 编写
- 下载 check.zip - 0.6 KB Matrix.h 文件
- 下载 Matrix.zip - 3.7 KB 一个程序,展示如何初始化类以及如何使用函数\运算符
引言
这个矩阵类是一个模板类,可以初始化为任何类型的数字类型(int、double、float 等)。它包括逆矩阵和行列式函数(编写代码并不容易),以及矩阵类所需的所有运算符,例如两个矩阵相乘或将数字乘以矩阵。它能够是任何大小,而且易于使用。
指令
上传的文件 check.zip 包含一个文件,展示了如何初始化矩阵以及运算符或函数如何工作。 请注意,括号可能是最重要的运算符。它允许您访问数组元素,如
matrix1
(2,0)=5; Matrix 类的成员函数
这些是该类的所有成员函数和运算符
template<class T>class Matrix
{
public
- Matrix(int Row_Size,int Column_Size)
- Matrix() >
- Matrix(const Matrix<T> &)
- ~Matrix(void)
- void Set_Element(int Row_,int Column,const T& Value)
- T Get_Element(int Row ,int Column) const
- bool operator!() const;
- const Matrix<T> & operator=(const Matrix<T> &)
- const Matrix<T> & operator=(const T&)
- bool operator==(const Matrix<T> &) const
- bool operator==(const T & ) const
- bool operator!=(const Matrix<T> & second) const
- bool operator!=(const T & number)
- T &operator() (int Row,int Column)
- Matrix<T> operator+(const T &)
- Matrix<T> operator+(const Matrix<T> &)
- const Matrix<T> &operator+=(const T&)
- const Matrix<T> & operator+=(const Matrix<T> &)
- Matrix<T> operator-(const T&)
- Matrix<T> operator - (const Matrix<T> &)
- const Matrix<T> & operator -=(const T & )
- const Matrix<T> & operator -=(Matrix<T> & )
- Matrix<T> operator*(const T&)
- Matrix<T> operator* (const Matrix<T> &)
- const Matrix<T> & operator*=(const T&)
- const Matrix<T> & operator*=(const Matrix<T> &)
- void Transposed();>
- Matrix<T> GetTransposed() const
- bool Inverse();>
- Matrix<T> GetInversed() const
- double Det() const
- void Set_Size(int RowSize,int ColumnSize)
- const int& Get_ColumnSize() const
- const int & Get_RowSize() const
- int m_RowSize
- int m_ColumnSize
- T**m_Data
};
注释
注意:Inverse 的返回参数(一个布尔值)显示矩阵是否可逆,如果可逆,则求矩阵的逆矩阵
注意:我已经在 vc 2005 中编写了此代码,但它不是托管代码。如果在 vc6 中编译它,您会看到一些错误,例如 i 被重新声明。这是因为 vc6 有点不同,当您这样声明 i 时:for (int i=0;i<5;i++){ /*语句*/}
在 for 语句之后 i 被销毁,但在 vc6 中即使在 for 语句之后它也是一个声明的变量。但是通过调试 vc 中的此类错误很容易将其更改为 vc6。你可以自己做。
注意:我唯一没有自己编写的代码是行列式,因为我找到了由 Paul Bourke 编写的非常棒的代码。要查找原始代码,请转到 http://local.wasp.uwa.edu.au/~pbourke/other/determinant/
类中使用的一些技术
为了声明一个,我必须指出使用 new 命令声明元素数组 (m_Data) 的方式
假设给出了 m_RowSize & m_ColumnSize
m_Data=new T* [m_RowSize];
for(int i=0;i<m_RowSize;i++)
m_ColumnSize=new T [m_ColumnSize];