C++矩阵类的实现

  
#ifndef _ARRAY1D
#define _ARRAY1D
#include<iostream.h>

/* 矩阵类*/
template<class T>
class CMatrix {
	public :
CMatrix(int r = 0, int c = 0);
CMatrix(const CMatrix<T>& m); //复制构造函数
~CMatrix() {delete [] element;}
int Rows() const {return rows;}
int Columns() const {return cols;}
T& operator()(int i, int j) const;
CMatrix<T>& operator=(const CMatrix<T>& m);
CMatrix<T> operator+() const; // 一元加法
CMatrix<T> operator+(const CMatrix<T>& m) const;
CMatrix<T> operator-() const; //一元减法
CMatrix<T> operator-(const CMatrix<T>& m) const;
CMatrix<T> operator*(const CMatrix<T>& m) const;
CMatrix<T>& operator+=(const T& x);
friend ostream& operator<<(ostream& out, const CMatrix<T>& w);
private :
int rows, cols; // 矩阵维数
T *element; // 元素数组
};
template<class T>
CMatrix<T>::CMatrix(int r, int c)
{// 类CMatrix的构造函数
// 验证r和c的合法性
if (r >0 || c>0) 
{
// 创建矩阵
rows = r;
cols=c;
element = new T [r*c];
}
}
template<class T>
T& CMatrix<T>::operator()(int i, int j) const
{// 返回一个指向元素( i , j )的引用
return element[(i-1)*cols+j-1];
}
template<class T>
CMatrix<T> CMatrix<T>::operator-(const CMatrix<T>& m) const
{// 返回(*this) - m.
if (rows==m.rows&&cols== m.cols)
{

// 创建结果矩阵w
CMatrix<T> w(rows, cols);
for (int i = 0; i < rows * cols; i++)
w.element[i]=element[i]-m.element[i];
return w;
}
}
template <class T>
ostream& operator<<(ostream& out, const CMatrix<T>& w)
{
    for(int i=0;i<w.rows*w.cols;i++)
	{
		out<<w.element[i]<<" ";
		if((i+1)%w.cols==0)
			out<<endl;
	}
	return out;
}
#endif


主函数的调用:

#include "Array1D.h"
void main()
{
         CMatrix<int> matrix(2, 3);
	matrix(1,1)=3;
	matrix(1,2)=10;
	matrix(1,3)=24;
	matrix(2,1)=20;
	matrix(2,2)=15;
	matrix(2,3)=27;
	cout<<matrix(1,3)<<endl;
	cout<<"所有矩阵的值为:"<<endl;
	cout<<matrix<<endl;

}


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,下面是一个简单的 C++ 矩阵实现矩阵相乘的示例代码: ```c++ #include <iostream> #include <vector> using namespace std; class Matrix { private: vector<vector<int>> data; // 存储矩阵的二维向量 int m, n; // 矩阵的行数和列数 public: // 构造函数 Matrix(int rows, int cols) { m = rows; n = cols; data.resize(m, vector<int>(n, 0)); // 初始化矩阵为全零矩阵 } // 矩阵相乘运算符重载 Matrix operator*(const Matrix& other) { if (n != other.m) { cout << "矩阵维度不匹配,无法相乘" << endl; return Matrix(0, 0); } Matrix result(m, other.n); for (int i = 0; i < m; i++) { for (int j = 0; j < other.n; j++) { int temp = 0; for (int k = 0; k < n; k++) { temp += data[i][k] * other.data[k][j]; } result.data[i][j] = temp; } } return result; } // 矩阵赋值运算符重载 Matrix& operator=(const Matrix& other) { if (this == &other) { return *this; } m = other.m; n = other.n; data = other.data; return *this; } // 打印矩阵 void print() { for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { cout << data[i][j] << " "; } cout << endl; } } }; int main() { Matrix A(2, 3); Matrix B(3, 2); A.print(); B.print(); A = {{1, 2, 3}, {4, 5, 6}}; B = {{7, 8}, {9, 10}, {11, 12}}; A.print(); B.print(); Matrix C = A * B; C.print(); return 0; } ``` 这个示例代码中,我们定义了一个 `Matrix` 来表示矩阵,并实现矩阵相乘的运算符重载。在 `main()` 函数中,我们创建了两个矩阵 `A` 和 `B`,并将它们赋值为我们需要相乘的矩阵。然后,我们通过 `A * B` 的方式来计算它们的乘积,并将结果存储在 `C` 中。最后,我们打印了 `C` 的值,即矩阵相乘的结果。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值