CMU15-455 PROJECT #0 - C++ PRIMER (实现一个矩阵类详解)

问题:为什么二维数组要转化为一维数组(一维数组连续存储,利用CPU Cache)

 Matrix就是相当于连续内存空间(一维数组), 最好初始化

对于行优先存储矩阵,我们用指针数组,每个指针指向该行的首地址,然后在原来线性的内存上操作。剩下的就不难实现。

同时该类实现使用到智能指针Unique_Point来管理矩阵。

#pragma once

#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[rows*cols];
    memset(linear,0,sizeof(T)*rows*cols);
  }
  // # of rows in the matrix
  int rows;
  // # of Columns in the matrix
  int cols;
  // Flattened array containing the elements of the matrix
  // 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 T GetElem(int i, int j) = 0;

  // Sets
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值