This repository has been archived on 2024-01-06. You can view files and clone it, but cannot push or open issues or pull requests.
justhomework/SoftwareDesign/Code/inc/matrix.hpp

54 lines
1.1 KiB
C++
Raw Normal View History

2022-03-11 12:27:54 +00:00
#ifndef _MATRIX_HPP_
#define _MATRIX_HPP_
#include "../inc/vector.hpp"
template <class T>
class Matrix
{
private:
int _height;
int _width;
Vector<T> *_mat;
protected:
public:
// 确定矩阵元素大小,不初始化
Matrix(int w, int h);
// 以相同值初始化矩阵元素
Matrix(int w, int h, T init_value);
// 以顺序访问数组的值初始化矩阵元素
Matrix(int w, int h, const double *elements);
// 以另一个矩阵初始化当前矩阵的元素,实际是元素拷贝
Matrix(const Matrix &another);
//只读访问内容
const T get(int w, int h) const;
//运算符重载
T &operator[](int i) { return _mat[i]; }
};
template <class T>
Matrix<T>::Matrix(int w, int h)
{
_width = w;
_height = h;
_mat = new Vector<T>(w * h);
}
template <class T>
Matrix<T>::Matrix(int w, int h, T init_value)
{
_width = w;
_height = h;
_mat = new Vector<T>(w * h, init_value);
}
template <class T>
const T Matrix<T>::get(int w, int h) const
{
return _mat[w * _width + h];
}
#endif