c++ - C ++如何编写一个采用两个矩阵并计算行数和列数的方法
问题描述
我正在编写一个矩阵处理类来练习该语言。
我的第一个方法是这样的:
template<typename T>
T* matrixClass<T>::CreateMat(unsigned int nRow, unsigned int nCol, T& num)
{
matrixClass::m_nRows = nRow; //number of rows
matrixClass::m_nCol = nCol; //number of columns
matrix = new T[nRow * nCol];//memory allocation
for (unsigned int row = 0; row < nRow; row++) //row selection N x m
{
for (unsigned int col = 0; col < nCol; col++)
{
matrix[col + row * nCol] = num; //assigns data to columns n x M
num += 1.1;
}
}
return matrix;
}
我正在尝试在同一个类中编写一个方法,该方法采用此方法创建的两个矩阵并找到它的 rows 和 columns 。
解决方案
这些类型的典型形状类似于
template <class T>
class Matrix {
private:
unsigned int m_nRows, m_nCols;
T* m_Data;
public:
Matrix() = delete;
Matrix(unsigned int nRow, unsigned int nCol, T* src = nullptr);
Matrix(const Matrix& orig);
Matrix(Matrix&& orig);
Matrix& operator=(const Matrix& orig);
Matrix& operator=(Matrix&& orig);
~Matrix();
unsigned int rows() const { return m_nRows; }
unsigned int columns() const { return m_nCols; }
}
...或将构造函数设为私有并createMat
接管构造Matrix
实例的角色。使用这种方法可以发现(或学习)很多边缘案例,所以玩得开心:)
推荐阅读
- xml - 是否可以从子节点复制数据并将其传递给父节点并使用 XSLT 删除子节点
- ethereum - Solidity - 添加总供应量
- sql - 我试图弄清楚我需要在sql命令oracle上写什么
- css - 如何制作多个高度相同的div
- typescript - 重新输入函数表达式的对象,跳过第一个函数参数
- netsuite - 客户端脚本用于收集新创建记录的 id 并将其设置在另一条记录上
- python - 如何将列表的值拆分为两个列表
- r - 启动错误:未使用的参数(原始),但正在使用参数
- android - 我在设置 Flutter 时遇到问题。我无法解决“AVD Pixel_3_API30 的模拟器进程被杀死。”
- microsoft-graph-api - 在 Microsoft 365 中自定义个人资料卡