0

我以为我已经为我initializer-list constructor的 . 它应该是overloaded constructor我的,从这种类型的输入Matrix class中创建一个:我知道我的一个定义是不正确的,但我无法破译哪个(S)。MatrixMatrix d = {{1,3}, {5,9};

Matrix::Matrix(const i_list & list){
  uint rows = list.size();
  uint cols = list.begin()->size();
  int i = 0;
  mat = new double*[rows];
  for(uint m = 0; m < rows; m++){
    mat[m] = new double[rows];
  }
  for(uint n = 0; n < rows; k++){
    for(uint w = 0; w < cols; w++){
      mat[n][w] = *(list.begin()[n].begin[w] + i);
      i++;
    }
  }
4

1 回答 1

2

这条线mat[n][w] = *(list.begin()[n].begin[w] + i);基本上是胡说八道。您可以循环输入,这意味着您不需要单独的循环。

Matrix::Matrix(const i_list & list){
  mat = new double*[list.size()];
  for(auto r = list.begin(); r != list.end(); ++r){
    auto row = mat[r - list.begin()] = new double[r->size()];
    for(auto c = r->begin(); c != r->end(); ++c){
      row[c - r->begin()] = *c;
    }
  }
}

但是您应该做的是mat从 a更改**double为 a std::vector<std::vector<double>>,此时它变为:

Matrix::Matrix(const i_list & list) : mat(list) {}
于 2017-10-13T16:13:46.457 回答