急求用c++如何实现矩阵的定义和单位矩阵的定义

如题所述

#include <iostream>
#include <vector>
#include <cassert>

using namespace std;

template < class T >
class CMatrix
{
public: //------------------ 构造部 -------------------------
CMatrix( void );
CMatrix( unsigned h, unsigned w );
CMatrix( const CMatrix& m );
CMatrix( const vector<T>& vec );
CMatrix( const vector<T>& vec,unsigned h,unsigned w );
~CMatrix(void);
private: //------------------- 数据部 ---------------------------
vector<T> m_vec_data;
unsigned m_u_width;
unsigned m_u_height;

public: // ------------------- 重载运算符 -------------------------

/// --- 以下均要求T具有如下运算能力:+ - += 等用到的。。。。

//取值运算
T& operator() ( unsigned row, unsigned col );
T operator() ( unsigned row, unsigned col ) const;

//赋值运算
CMatrix& operator = ( const CMatrix& );
CMatrix& operator += ( const CMatrix& );
CMatrix& operator -= ( const CMatrix& );
CMatrix& operator *= ( T );
CMatrix& operator /= ( T );

//二元运算符
CMatrix operator + ( const CMatrix& ) const;
CMatrix operator - ( const CMatrix& ) const;
CMatrix operator * ( const CMatrix& ) const;
CMatrix operator * ( T ) const;
CMatrix operator / ( T ) const;

bool operator == ( const CMatrix& ) const;
bool operator != ( const CMatrix& ) const;

public: // -------------------- 操作函数部 -------------------------
void transpose();
inline bool empty();
inline long size();
inline unsigned height();
inline unsigned width();

public: // ------------------ 输入输出 ----------------------
// 注意:矩阵元素必须支持输入输出,否则程序会错误!!
/*friend ostream& operator << ( ostream& os,const CMatrix& ma );*/
template < typename T > friend ostream& operator << ( ostream& os,const CMatrix<T>& ma );
//friend istream& operator >> ( istream& is,CMatrix& ma );
template < class T > friend istream& operator >> ( istream& is,CMatrix<T>& ma );
};

自己修改成方正即可。。。
温馨提示:答案为网友推荐,仅供参考
相似回答