矩阵类设计

GitHub

用C++类封装了一个矩阵的类,实现了加、减和乘法的运算符重载,同时复习了一遍拷贝、移动等构造函数和赋值运算符的重载。

Matrix.h

#pragma once
#include 
#include 

template
class Matrix
{
public:
	Matrix()
	{
		Row = Column = 0;
		Item = nullptr;
	}
	Matrix(int row, int column)
	{
		Create(row, column);
	}
	Matrix(const Matrix& other)
	{
		Row = other.Row;
		Column = other.Column;
		Item = new T * [Row];
		for (int i = 0; i < Row; i++)
		{
			Item[i] = new T[Column];
			::memcpy(Item[i], other.Item[i], sizeof(T) * Column);
		}
	}
	Matrix& operator=(const Matrix& other)
	{
		Row = other.Row;
		Column = other.Column;
		Item = new T * [Row];
		for (int i = 0; i < Row; i++)
		{
			Item[i] = new T[Column];
			::memcpy(Item[i], other.Item[i], sizeof(T) * Column);
		}
		return *this;
	}
	Matrix(Matrix&& other)
	{
		Row = other.Row;
		Column = other.Column;
		Item = other.Item;
		other.Item = nullptr;
	}
	Matrix& operator=(Matrix&& other)
	{
		Row = other.Row;
		Column = other.Column;
		Item = other.Item;
		other.Item = nullptr;
	}
	~Matrix()
	{
		Release();
	}
	void Create(int row, int column)
	{
		Row = row;
		Column = column;
		Item = new T * [row];
		for (int i = 0; i < row; i++)
		{
			Item[i] = new T[column];
		}
	}
	void Release()
	{
		if (Item != nullptr)
		{
			for (int i = 0; i < Row; i++)
			{
				delete[] Item[i];
			}
			delete[] Item;
		}
		Item = nullptr;
		Row = 0;
		Column = 0;
	}
	void Resize(int row, int column)
	{
		if (row == Row && column == Column)
			return;
		Release();
		Create(row, column);
	}

	int* operator[](int i)
	{
		return Item[i];
	}
	Matrix operator+(const Matrix& other)
	{
		if (other.Row != Row || other.Column != Column)
			return Matrix();
		Matrix result(Row, Column);
		for (auto i = 0; i < Row; i++)
		{
			for (auto j = 0; j < Column; j++)
			{
				result[i][j] = Item[i][j] + other.Item[i][j];
			}
		}
		return std::move(result);
	}
	Matrix operator-(const Matrix& other)
	{
		if (other.Row != Row || other.Column != Column)
			return Matrix();
		Matrix result(Row, Column);
		for (auto i = 0; i < Row; i++)
		{
			for (auto j = 0; j < Column; j++)
			{
				result[i][j] = Item[i][j] - other.Item[i][j];
			}
		}
		return std::move(result);
	}
	Matrix operator*(const Matrix& other)
	{
		if (Column != other.Row)
			return Matrix();
		Matrix result(Row, other.Column);
		for (auto i = 0; i < Row; i++)
		{
			for (auto j = 0; j < other.Column; j++)
			{
				T key = 0;
				for (auto k = 0; k < Column; k++)
				{
					key += Item[i][k] * other.Item[k][j];
				}
				result[i][j] = key;
			}
		}
		return std::move(result);
	}
	int GetRow() { return Row; }
	int GetColumn() { return Column; }
	bool SetRowValue(int row, int* values)
	{
		if (row >= Row)
			return false;
		for (int i = 0; i < Column; i++)
		{
			Item[row][i] = values[i];
		}
		return true;
	}
	void SetValue(int** values)
	{
		for(auto i = 0; i < Row; i++)
			for (auto j = 0; j < Column; j++)
			{
				Item[i][j] = values[i][j];
			}
	}
	void Print(std::string name)
	{
		printf("%s:\n", name.c_str());
		for (int i = 0; i < Row; i++)
		{
			for (int j = 0; j < Column; j++)
			{
				printf("%d\t", Item[i][j]);
			}
			printf("\n");
		}
		printf("\n");
	}
private:
	int Row;
	int Column;

	T** Item;
};


void TestMatrix();

Matirx.cpp

#include "Matrix.h"

using namespace std;

void TestMatrix()
{
	Matrix m1(2,2), m2(2, 2);
	int a[2] = { 1,2 };
	for (auto i = 0; i < 2;i++)
	{
		m1.SetRowValue(i, a);
		m2.SetRowValue(i, a);
	}
	m1.Print("m1");
	m2.Print("m2");
	Matrix m3 = m1 + m2;
	m3.Print("m3");
	Matrix m4 = m1 - m2;
	m4.Print("m4");

	Matrix m5(2, 3), m6(3, 2);
	int b[3] = { 1, 2, 3 };
	int c[2] = { 1, 2 };
	m5.SetRowValue(0, b);
	m5.SetRowValue(1, b);
	m6.SetRowValue(0, c);
	m6.SetRowValue(1, c);
	m6.SetRowValue(2, c);
	Matrix m7 = m5 * m6;
	m5.Print("m5");
	m6.Print("m6");
	m7.Print("m7");

	Matrix m8(2, 3);
	int** p = new int* [2];
	p[0] = new int[3]{ 1, 2, 3 };
	p[1] = new int[3]{ 4, 5,6 };
	m8.SetValue(p);
	m8.Print("m8");

	Matrix m9(m8), m10;
	m10 = m8;
	m9.Print("m9");
	m10.Print("m10");
}

测试用例输出结果:

m1:
1       2
1       2

m2:
1       2
1       2

m3:
2       4
2       4

m4:
0       0
0       0

m5:
1       2       3
1       2       3

m6:
1       2
1       2
1       2

m7:
6       12
6       12

m8:
1       2       3
4       5       6

m9:
1       2       3
4       5       6

m10:
1       2       3
4       5       6


E:\GitHub\IntroductionToAlgorithms\C++\build\Debug\IntroductionToAlgorithms.exe (进程 14088)已退出,代码为 0。
要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口. . .


 

你可能感兴趣的:(算法导论,紫云的程序人生,算法导论,矩阵,拷贝与移动)