将2D向量转换为2D数组 [英] Converting 2D vector to 2D array
本文介绍了将2D向量转换为2D数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
自从我上次访问数组以来已经有一段时间了(最近我一直在使用向量),由于我使用的库,我需要将2D向量转换回2D数组,因为我接受类型为的参数double数组
,其中该数组的访问者例如是 foo [i] [j]
.
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.
这是我的代码:
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];
}
}
}
因此,我得到错误:'M'不能出现在常量表达式中
我也尝试了以下方法:
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];
}
}
}
但是,这会产生分段错误11.
However, this produces a segmentation fault 11.
任何人都可以提出任何建议,还是将向量转换为2D数组的更好方法.
Could anyone suggest any advice, or, a better way to convert a vector to a 2D array..
谢谢
推荐答案
您已经关闭.应该是:
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];
}
}
}
这篇关于将2D向量转换为2D数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文