我有
main(){...
float **tree;
//How to set some values here for e.g. If I want tree to be a 15x2 array of some values?
reprVectorsTree *r1 = new reprVectorsTree(tree,8,2);
...}
reprVectorsTree(float **tree, int noOfReprVectors, int dimensions)
{.....
如何在这里使用 malloc 以便我可以在树数组中设置一些数据?
最佳答案
要为 tree
分配内存,请尝试如下操作:
float** tree;
tree = (float**)malloc(15 * sizeof(float*));
for(i = 0; i < 15; i++)
tree[i] = (float*)malloc(2 * sizeof(float));
现在您可以设置值:
for(i = 0; i < 15; i++)
for(j = 0; j < 2; j++)
tree[i][j] = 2;
不要忘记稍后free
,虽然我不明白你为什么要将new
和malloc
结合在一起?
关于c++ - 如何在多维数组中输入值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11984994/