【面向对象程序设计实践】矩阵模板类的设计

这篇博客介绍了如何使用C++模板类实现一个支持多种操作的矩阵类,包括初始化、转置、排序、加减运算以及与数值的乘除运算。同时,展示了使用二维指针和vector两种不同的数据结构来实现矩阵类,详细阐述了每个成员函数的功能和运算符重载的使用。此外,还讨论了类的内存管理和效率问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

编写一个整型数的矩阵类。满足以下要求:

可以根据变量m,n来创建m行,n列大小的矩阵,并初始化所有元素值为指定值;

从键盘输入矩阵所有的元素值;

矩阵转置;

对矩阵元素以列的方式进行升序排序;

对矩阵元素以行的方式进行降序排序;

矩阵(相同行列数)的加法(对应位置元素相加);

矩阵(相同行列数)的减法(对应位置元素相减)。

可以用文件流输入输出来装入和保存矩阵。

之后,添加了以下操作符重载

 + - * / >> << =

进一步要求:IMatrix类改写为模板矩阵类,并编写代码测试运行#

用二维指针实现版:

#include<iostream>
#include<iomanip>
using namespace std;
template<class Value>
class Matrix {
public:
	Matrix(int, int);//
	Matrix(const Matrix&);//
	~Matrix();//
	void search(int, int);//
	void transrer();//
	void column_rank(int);//
	void row_rank(int);//
	friend Matrix<Value>  operator + (const Matrix<Value>& a, const Matrix<Value>& b) {
		Matrix<Value> c(a.m_length, a.m_width);
		if (a.m_length != b.m_length || a.m_width != b.m_width) {
			cout << "error" << endl;
		}
		else {
			for (int i = 0; i < c.m_length; i++) {
				for (int j = 0; j < c.m_width; j++) {
					c.m_matrix[i][j] = a.m_matrix[i][j] + b.m_matrix[i][j];
				}
			}
		}
		return c;
	}
	friend Matrix<Value> operator - (const Matrix<Value>& a, const Matrix<Value>& b) {
		Matrix<Value> c(a.m_length, b.m_width);
		if (a.m_length != b.m_length || a.m_width != b.m_width) {
			cout << "error" << endl;
		}
		else {
			for (int i = 0; i < c.m_length; i++) {
				for (int j = 0; j < c.m_width; j++) {
					c.m_matrix[i][j] = a.m_matrix[i][j] - b.m_matrix[i][j];
				}
			}
		}
		return c;
	}
	template<class num>
	friend Matrix<Value> operator * (num d, const Matrix<Value>& a) {
		for (int i = 0; i < a.m_length; i++) {
			for (int j = 0; j < a.m_width; j++) {
				a.m_matrix[i][j] *= d;
			}
		}
		return a;
	}
	template<class num>
	friend Matrix<Value> operator / (const Matrix<Value>& a, num d) {
		for (int i = 0; i < a.m_length; i++) {
			for (int j = 0; j < a.m_width; j++) {
				a.m_matrix[i][j] /= d;
			}
		}
		return a;
	}
	Matrix<Value>& operator = (const Matrix<Value>&);//
	friend ostream& operator << (ostream& out, Matrix<Value>& a) {
		for (int i = 0; i < a.m_length; i++) {
			for (int j = 0; j < a.m_width; j++) {
				cout << setw(3) << a.m_matrix[i][j];
			}
			cout << endl;
		}
		return out;
	}
	friend istream& operator >>(istream& in, Matrix<Value>& a) {
		for (int i = 0; i < a.m_length; i++) {
			for (int j = 0; j <a.m_width; j++) {
				cin >> a.m_matrix[i][j];
			}
		}
		return in;
	}
	
private:
	void rank(int, int);//
	Value m_length, m_width;
	Value** m_matrix;
};
template<class Value>
Matrix<Value>::Matrix(int a, int b) {
	m_length = a;
	m_width = b;
	m_matrix = new Value *[m_length];
	for (int i = 0; i < m_length; i++) {
		m_matrix[i] = new Value[m_width];
		for (int j = 0; j < m_width; j++) {
			m_matrix[i][j] = 0;
		}
	}
}
template<class Value>
Matrix<Value>::Matrix(const Matrix<Value>& a) {
	m_length = a.m_length;
	m_width = a.m_width;
	m_matrix = new Value * [m_length];
	for (int i = 0; i < m_length; i++) {
		m_matrix[i] = new Value[m_width];
		for (int j = 0; j < m_width; j++) {
			m_matrix[i][j] = a.m_matrix[i][j];
		}
	}
}
template<class Value>
Matrix<Value>::~Matrix() {
	for (int i = 0; i < m_length; i++) {
		delete[] m_matrix[i];
	}
	delete[] m_matrix;
}
template<class Value>
void Matrix<Value>::search(int a, int b) {
	if (a > m_length || b > m_width)cout << "输入错误" << endl;
	else cout << m_matrix[a][b] << endl;
}
template<class Value>
void Matrix<Value>::transrer() {
	int a = m_length;
	m_length = m_width;
	m_width = a;
	int** m_temp = new Value* [m_length];
	for (int i = 0; i < m_length; i++) {
		m_temp[i] = new Value[m_width];
	}

	for (int j = 0; j < m_width; j++) {
		for (int k = 0; k < m_length; k++) {
			m_temp[k][j] = m_matrix[j][k];
		}
	}
	for (int i = 0; i < m_width; i++) {
		delete[] m_matrix[i];
	}
	delete[] m_matrix;
	m_matrix = m_temp;
	m_temp = nullptr;
}
template<class Value>
void Matrix<Value>::column_rank(int a) {
	rank(1, a);
}
template<class Value>
void Matrix<Value>::row_rank(int a) {
	rank(2, a);
}
template<class Value>
void Matrix<Value>::rank(int sign, int a) {
	switch (sign) {
	case 1:
		for (int i = m_length; i > 1; i--) {
			for (int j = 0; j < (i - 1); j++) {
				if (m_matrix[j][a] < m_matrix[j + 1][a]) {
					int b = m_matrix[j][a];
					m_matrix[j][a] = m_matrix[j + 1][a];
					m_matrix[j + 1][a] = b;
				}
			}
		}
		break;
	case 2:
		for (int i = m_width; i > 1; i--) {
			for (int j = 0; j < (i - 1); j++) {
				if (m_matrix[a][j] < m_matrix[a][j + 1]) {
					int b = m_matrix[a][j];
					m_matrix[a][j] = m_matrix[a][j + 1];
					m_matrix[a][j + 1] = b;
				}
			}
		}


	}
}
template<class Value>
Matrix<Value>& Matrix<Value>::operator = (const Matrix<Value>& a) {
	if (this == &a) {
		return *this;
	}
	for (int i = 0; i < a.m_length; i++) {
		for (int j = 0; j < a.m_width; j++) {
			m_matrix[i][j] = a.m_matrix[i][j];
		}
	}
	return *this;
}

用vector实现版:实际上几乎没有区别,主要的区别在析构函数。 

#include<iostream>
#include<iomanip>
#include<vector>
using namespace std;
template<class Value>
class Matrix {
public:
	Matrix(int, int);
	Matrix(const Matrix&);
	void transrer();
	void column_rank(int);
	void row_rank(int);
	friend Matrix<Value>  operator + (const Matrix<Value>& a, const Matrix<Value>& b) {
		Matrix<Value> c(a.m_length, a.m_width);
		if (a.m_length != b.m_length || a.m_width != b.m_width) {
			cout << "error" << endl;
		}
		else {
			for (int i = 0; i < c.m_length; i++) {
				for (int j = 0; j < c.m_width; j++) {
					c.m_data[i][j] = a.m_data[i][j] + b.m_data[i][j];
				}
			}
		}
		return c;
	}
	friend Matrix<Value> operator - (const Matrix<Value>& a, const Matrix<Value>& b) {
		Matrix<Value> c(a.m_length, b.m_width);
		if (a.m_length != b.m_length || a.m_width != b.m_width) {
			cout << "error" << endl;
		}
		else {
			for (int i = 0; i < c.m_length; i++) {
				for (int j = 0; j < c.m_width; j++) {
					c.m_data[i][j] = a.m_data[i][j] - b.m_data[i][j];
				}
			}
		}
		return c;
	}
	template<class num>
	friend Matrix<Value> operator * (num d,  Matrix<Value>& a) {
		for (int i = 0; i < a.m_length; i++) {
			for (int j = 0; j < a.m_width; j++) {
				a.m_data[i][j] *= d;
			}
		}
		return a;
	}
	template<class num>
	friend Matrix<Value> operator / ( Matrix<Value>& a, num d) {
		for (int i = 0; i < a.m_length; i++) {
			for (int j = 0; j < a.m_width; j++) {
				a.m_data[i][j] /= d;
			}
		}
		return a;
	}
	Matrix<Value>& operator = (const Matrix<Value>&);
	friend ostream& operator << (ostream& out, Matrix<Value>& a) {
		for (int i = 0; i < a.m_length; i++) {
			for (int j = 0; j < a.m_width; j++) {
				cout << setw(3) << a.m_data[i][j];
			}
			cout << endl;
		}
		return out;
	}
	friend istream& operator >>(istream& in, Matrix<Value>& a) {
		for (int i = 0; i < a.m_length; i++) {
			for (int j = 0; j < a.m_width; j++) {
				cin >> a.m_data[i][j];
			}
		}
		return in;
	}
private:
	void rank(int, int);
	Value m_length, m_width;
	vector< vector<Value> >m_data;
};
template<class Value>
Matrix<Value>::Matrix(int a, int b) {
	m_length = a;
	m_width = b;
	m_data.resize(a);
	for (int i = 0; i < m_length; i++) {
		m_data[i].resize(b);
		for (int j = 0; j < m_width; j++) {
			m_data[i][j] = 0;
		}
	}
}
template<class Value>
Matrix<Value>::Matrix(const Matrix<Value>& a) {
	m_length = a.m_length;
	m_width = a.m_width;
	m_data.resize(m_length);
	for (int i = 0; i < m_length; i++) {
		m_data[i].resize(m_width);
		for (int j = 0; j < m_width; j++) {
			m_data[i][j] = a.m_data[i][j];
		}
	}
}
template<class Value>
void Matrix<Value>::transrer() {
	int a = m_length;
	m_length = m_width;
	m_width = a;
	vector< vector<Value> >m_temp(m_length,vector<Value>(m_width,0));
	for (int i = 0; i < m_length; i++) {
	}
	for (int j = 0; j < m_width; j++) {
		for (int k = 0; k < m_length; k++) {
			m_temp[k][j] = m_data[j][k];
		}
	}
	m_data = m_temp;
}
template<class Value>
void Matrix<Value>::column_rank(int a) {
	rank(1, a);
}
template<class Value>
void Matrix<Value>::row_rank(int a) {
	rank(2, a);
}
template<class Value>
void Matrix<Value>::rank(int sign, int a) {
	switch (sign) {
	case 1:
		for (int i = m_length; i > 1; i--) {
			for (int j = 0; j < (i - 1); j++) {
				if (m_data[j][a] < m_data[j + 1][a]) {
					int b = m_data[j][a];
					m_data[j][a] = m_data[j + 1][a];
					m_data[j + 1][a] = b;
				}
			}
		}
		break;
	case 2:
		for (int i = m_width; i > 1; i--) {
			for (int j = 0; j < (i - 1); j++) {
				if (m_data[a][j] < m_data[a][j + 1]) {
					int b = m_data[a][j];
					m_data[a][j] = m_data[a][j + 1];
					m_data[a][j + 1] = b;
				}
			}
		}


	}
}
template<class Value>
Matrix<Value>& Matrix<Value>::operator = (const Matrix<Value>& a) {
	if (this == &a) {
		return *this;
	}
	for (int i = 0; i < a.m_length; i++) {
		for (int j = 0; j < a.m_width; j++) {
			m_data[i][j] = a.m_data[i][j];
		}
	}
	return *this;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值