一个C++矩阵类的实现
#ifndef _MATRIX_H_
#define _MATRIX_H_
#include <vector>
using std::vector;
/*类功能:矩阵类
*原理:采用数组的数组来实现
*/
template<typename T>
class SeqMatrix
{
public:
explicit SeqMatrix(size_t row=0,size_t col=0)
:arrayList(row)
{
for(size_t i=0;i<row;++i)
arrayList[i].resize(col);
}
SeqMatrix(const SeqMatrix &matrix)
:arrayList(matrix.rows())
{
for(size_t i=0;i<matrix.rows();++i)
{
arrayList[i].resize(matrix.colums());
arrayList[i]=matrix[i];
}
}
virtual ~SeqMatrix()
{
}
void resize(size_t row,size_t col)
{
arrayList.resize(row);
for(size_t i=0;i<row;++i)
arrayList[i].resize(col);
}
const size_t rows() const
{
return arrayList.size();
}
const size_t colums() const
{
if(arrayList.size()>0)
return arrayList[0].size();
return 0;
}
vector<T>& operator[](size_t row)
{
return arrayList[row];
}
const vector<T>& operator[](size_t row) const
{
return arrayList[row];
}
SeqMatrix& operator=(const SeqMatrix &rhs)
{
if(this==&rhs)
return *this;
resize(rhs.rows(),rhs.colums());
for(size_t i=0;i<rhs.rows();++i)
arrayList[i]=rhs[i];
}
private:
vector<vector<T>> arrayList;
};
#endif