用模板技术,重新实现一个矩阵类CMatrixT,其它要求与上一个问题相同
时间: 2024-03-14 16:45:41 浏览: 26
好的,您的要求已经被记录下来了,我会为您回答这个问题。
以下是CMatrixT的模板类定义:
```c++
template<typename T, int ROW, int COL>
class CMatrixT {
private:
T m_array[ROW][COL];
public:
CMatrixT() {}
CMatrixT(const T& val) {
for (int i = 0; i < ROW; ++i) {
for (int j = 0; j < COL; ++j) {
m_array[i][j] = val;
}
}
}
CMatrixT(const T arr[ROW][COL]) {
for (int i = 0; i < ROW; ++i) {
for (int j = 0; j < COL; ++j) {
m_array[i][j] = arr[i][j];
}
}
}
CMatrixT(const CMatrixT& other) {
for (int i = 0; i < ROW; ++i) {
for (int j = 0; j < COL; ++j) {
m_array[i][j] = other.m_array[i][j];
}
}
}
CMatrixT& operator=(const CMatrixT& other) {
if (this != &other) {
for (int i = 0; i < ROW; ++i) {
for (int j = 0; j < COL; ++j) {
m_array[i][j] = other.m_array[i][j];
}
}
}
return *this;
}
CMatrixT operator+(const CMatrixT& other) const {
CMatrixT res;
for (int i = 0; i < ROW; ++i) {
for (int j = 0; j < COL; ++j) {
res.m_array[i][j] = m_array[i][j] + other.m_array[i][j];
}
}
return res;
}
CMatrixT operator-(const CMatrixT& other) const {
CMatrixT res;
for (int i = 0; i < ROW; ++i) {
for (int j = 0; j < COL; ++j) {
res.m_array[i][j] = m_array[i][j] - other.m_array[i][j];
}
}
return res;
}
template<int NEW_COL>
CMatrixT<T, ROW, NEW_COL> operator*(const CMatrixT<T, COL, NEW_COL>& other) const {
CMatrixT<T, ROW, NEW_COL> res;
for (int i = 0; i < ROW; ++i) {
for (int j = 0; j < NEW_COL; ++j) {
T sum = 0;
for (int k = 0; k < COL; ++k) {
sum += m_array[i][k] * other.m_array[k][j];
}
res.m_array[i][j] = sum;
}
}
return res;
}
CMatrixT operator*(const T& val) const {
CMatrixT res;
for (int i = 0; i < ROW; ++i) {
for (int j = 0; j < COL; ++j) {
res.m_array[i][j] = m_array[i][j] * val;
}
}
return res;
}
friend CMatrixT operator*(const T& val, const CMatrixT& m) {
return m * val;
}
bool operator==(const CMatrixT& other) const {
for (int i = 0; i < ROW; ++i) {
for (int j = 0; j < COL; ++j) {
if (m_array[i][j] != other.m_array[i][j]) {
return false;
}
}
}
return true;
}
bool operator!=(const CMatrixT& other) const {
return !(*this == other);
}
T* operator[](int row) {
return m_array[row];
}
const T* operator[](int row) const {
return m_array[row];
}
friend std::ostream& operator<<(std::ostream& os, const CMatrixT& m) {
for (int i = 0; i < ROW; ++i) {
for (int j = 0; j < COL; ++j) {
os << m.m_array[i][j] << ' ';
}
os << std::endl;
}
return os;
}
};
```
这里定义了一个名为`CMatrixT`的模板类,其中`T`是矩阵元素的数据类型,`ROW`和`COL`分别表示矩阵的行数和列数。这样,我们就可以定义任意数据类型及任意维度的矩阵。
在类的实现中,我们重载了一些运算符,例如加法、减法、乘法、赋值、下标等,以方便对矩阵进行操作。我们还定义了一些构造函数,例如默认构造函数、构造函数初始化所有元素为同一个值、从一个二维数组中构造矩阵等等。
最后,我们还重载了`<<`运算符,以便可以使用`std::cout`输出矩阵。
使用模板技术重新实现的`CMatrixT`类与上一个问题中的`CMatrix`类的功能相同,但可以支持不同的数据类型和矩阵维度。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)