这个练习比较容易,基本实现一个行优先矩阵类,按照模版封装即可,用到一些Cpp11的高级技巧,去https://en.cppreference.com/w/查即可。
注意内存分配的一些细节:
预计用时:1h
#include <memory>
namespace bustub {
/*
* The base class defining a Matrix
*/
template <typename T>
class Matrix {
protected:
Matrix(int r, int c):rows(r),cols(c) {
linear = new T[r * c];
memset(linear, 0 ,sizeof(T) * r * c);
}
// # of rows in the matrix
int rows;
// # of Columns in the matrix
int cols;
// Flattened array containing the elements of the matrix
// Allocate the array in the constructor. Don't forget to free up
// the array in the destructor.
T *linear;
public:
// Return the # of rows in the matrix
virtual int GetRows() = 0;
// Return the # of columns in the matrix
virtual int GetColumns() = 0;
// Return the (i,j)th matrix element
virtual