将2D矢量转换为2D数组

时间:2022-09-30 21:30:42

It's been a while since I last visited arrays (I've been working with vectors recently) and I need to convert an 2D vector back into a 2D array because of a library I am using accepts the paramaters of type double array where the accessors of this array is foo[i][j] for example.

自从我上次访问数组(我最近一直使用向量)已经有一段时间了,我需要将2D向量转换回2D数组,因为我正在使用的库接受类型为double数组的参数例如,这个数组是foo [i] [j]。

Here is my code:

这是我的代码:

double** setupHMM(vector<vector<double> > &vals, int N, int M)
{
  double** temp;
  temp = new double[N][M];

 for(unsigned i=0; (i < N); i++)
 {
    for(unsigned j=0; (j < M); j++)
    {
        temp[i][j] = vals[i][j];
    }
 }
}

And with this, I get error: ‘M’ cannot appear in a constant-expression

有了这个,我得到错误:'M'不能出现在常量表达式中

I have also tried the following:

我也尝试过以下方法:

double** setupHMM(vector<vector<double> > &vals, int N, int M)
{
   double** temp;

   for(unsigned i=0; (i < N); i++)
   { 
      temp[i] = new double[N];
      for(unsigned j=0; (j < M); j++)
      {
          temp[j] = new double[M];
          temp[i][j] = vals[i][j];
      } 
   }
 }

However, this produces a segmentation fault 11.

然而,这产生了分段故障11。

Could anyone suggest any advice, or, a better way to convert a vector to a 2D array..

任何人都可以提出任何建议,或者更好的方法将矢量转换为2D数组。

Thanks

3 个解决方案

#1


9  

You were close. It should be:

你很亲密它应该是:

double** setupHMM(vector<vector<double> > &vals, int N, int M)
{
   double** temp;
   temp = new double*[N];
   for(unsigned i=0; (i < N); i++)
   { 
      temp[i] = new double[M];
      for(unsigned j=0; (j < M); j++)
      {
          temp[i][j] = vals[i][j];
      } 
   }
 }

#2


6  

A double pointer (double**) is not convertible to a 2D array.

双指针(双**)不可转换为2D数组。

double** temp;
temp = new double[N][M];  //invalid


double** temp;
temp = new double(*)[M];

It's a common misunderstanding to think that because an 1D array decays to a pointer that therefore a 2D array will decay to a double pointer. This is not true. The decay only happens with a single pointer.

这是一个常见的误解,认为因为1D数组衰减到指针,因此2D数组将衰减为双指针。这不是真的。衰变只发生在一个指针上。

#3


1  

replace

temp[i] = new double[N];

with

temp = new double*[N];

in the second code, and move it outside the loop

在第二个代码中,并将其移出循环

#1


9  

You were close. It should be:

你很亲密它应该是:

double** setupHMM(vector<vector<double> > &vals, int N, int M)
{
   double** temp;
   temp = new double*[N];
   for(unsigned i=0; (i < N); i++)
   { 
      temp[i] = new double[M];
      for(unsigned j=0; (j < M); j++)
      {
          temp[i][j] = vals[i][j];
      } 
   }
 }

#2


6  

A double pointer (double**) is not convertible to a 2D array.

双指针(双**)不可转换为2D数组。

double** temp;
temp = new double[N][M];  //invalid


double** temp;
temp = new double(*)[M];

It's a common misunderstanding to think that because an 1D array decays to a pointer that therefore a 2D array will decay to a double pointer. This is not true. The decay only happens with a single pointer.

这是一个常见的误解,认为因为1D数组衰减到指针,因此2D数组将衰减为双指针。这不是真的。衰变只发生在一个指针上。

#3


1  

replace

temp[i] = new double[N];

with

temp = new double*[N];

in the second code, and move it outside the loop

在第二个代码中,并将其移出循环