目录
CMatrix(int nRow, int nCol, double *pData=NULL)
bool Create(int nRow, int nCol, double *pData=NULL)
一、构造函数
CMatrix()
不带参数的构造函数
CMatrix::CMatrix() :m_nRow(0), m_nCol(0), m_pData(NULL)//这种初始化效率更高
{
/*m_nRow = m_nCol = 0;
m_pData = NULL;*/
}
CMatrix(int nRow, int nCol, double *pData=NULL)
带行、列及数据指针等参数的构造函数,并且参数带默认值
CMatrix::CMatrix(int nRow, int nCol, double* pData)
{
m_pData = NULL;
Create(nRow,nCol,pData);
}
CMatrix(const char * strPath)
带文件路径参数的构造函数
CMatrix::CMatrix(const char* strPath)
{
m_pData = NULL;
ifstream is(strPath);
is >> *this;
}
CMatrix(const CMatrix& m):
拷贝构造函数
方法一:
CMatrix::CMatrix(const CMatrix& m)
{
m_nRow = m.m_nRow;
m_nCol = m.m_nCol;
m_pData = NULL;
Create(m_nRow, m_nCol, m.m_pData);
}
方法二:
CMatrix::CMatrix(const CMatrix& m):CMatrix(m.m_nRow,m.m_nCol,m.m_pData)
{
}
bool Create(int nRow, int nCol, double *pData=NULL)
先删除原有空间,根据传入行列创建空间,如果pData不为空要将pData的内容拷贝到m_pData中。
bool CMatrix::Create(int nRow, int nCol, double* pData)
{
Release();
m_nRow = nRow;
m_nCol = nCol;
m_pData = new double[nRow * nCol];//申请新空间
if (m_pData != NULL) {
if(pData != NULL){
memcpy(m_pData, pData, nRow * nCol * sizeof(double));
}
return true;
}
else
{
return false;
}
}
二、析构函数
~CMatrix()
调用Release()
CMatrix::~CMatrix()
{
Release();
}
Release()
将内存释放,并将行列设置为0
void CMatrix::Release()
{
if (m_pData!=NULL) {
delete[]m_pData;
m_pData = NULL;
}
m_nRow = m_nCol = 0;
}
三、友元函数
输入运算符:<<
istream& operator>>(istream& is, CMatrix& m)
{
is >> m.m_nRow >> m.m_nCol;
m.Create(m.m_nRow, m.m_nCol);
for (int i = 0; i < m.m_nRow*m.m_nCol; i++)
{
is >> m.m_pData[i];
}
return is;// TODO: 在此处插入 return 语句
}
输出运算符:>>
ostream& operator<<(ostream& os, const CMatrix& m)
{
os << m.m_nRow << " " << m.m_nCol << endl;
for (int i = 0; i < m.m_nRow; i++)
{
for (int j = 0; j < m.m_nCol; j++)
{
os << m.m_pData[i * m.m_nCol + j] << " ";
}
os << endl;
}
return os;// TODO: 在此处插入 return 语句
}
四、运算符重载
算术运算符重载:+, +=
CMatrix& CMatrix::operator+=(const CMatrix& m) {
assert(m_nRow == m.m_nRow && m_nCol == m.m_nCol);
for (int i = 0; i < m_nRow * m_nCol; i++) {
m_pData[i] += m.m_pData[i];
}
return *this;
}
CMatrix operator+(const CMatrix& m1, const CMatrix& m2) {
CMatrix m3(m1);
m3 += m2;
return m3;
}
关系运算符重载:==,!=
bool CMatrix::operator==(const CMatrix& m) {
if (!(m_nRow == m.m_nRow && m_nCol == m.m_nCol)) {
return false;
}
for (int i = 0; i < m_nRow * m_nCol; i++) {
if (m_pData[i] != m.m_pData[i]) {
return false;
}
}
return true;
}
bool CMatrix::operator!=(const CMatrix& m) {
return !((*this) == m);
}
下标操作符:[], ()
double& CMatrix::operator[](int nIndex) {
assert(nIndex < m_nRow * m_nCol);
return m_pData[nIndex];
}
double& CMatrix::operator()(int nRow,int nCol) {
assert(nRow*m_nCol+nCol<m_nRow*m_nCol);
return m_pData[nRow*m_nCol+nCol];
}
强制类型转换: double
CMatrix::operator double() {
double ds = 0;//返回值
for (int i = 0; i < m_nRow * m_nCol; i++) {
ds += m_pData[i];
}
return ds;
}
赋值运算符:=
尤其注意当m1=m1特殊情况的处理
CMatrix& CMatrix::operator=(const CMatrix& m) {
if (this != &m) {
Create(m.m_nRow, m.m_nCol, m.m_pData);
}
return *this;
}
main.cpp
#include <iostream>
#include "CComplex.h"
#include<stdio.h>
#include"CMatrix.h"
using namespace std;
int main(int argc, char* argv[]) {
CMatrix m;
double data[10] = { 1,2,3,4,5,6 };
{
CMatrix m2(2,5,data);
}
CMatrix m1(2,5, data);
CMatrix ms[3] = { CMatrix(),CMatrix(2,5,data),"c://Users//why-hahaha//Desktop//1.txt" };
CMatrix m4;
CMatrix m3(m1);
m4 = m1;
m1.Set(0, 1, 20);
cout << "m1=" << dec << m1;
cout << "m3=" << m3;
cout << "m4=" << m4;
return 0;
}
结果: