我正在尝试制作一个包含大量数据的多维数组。
但是,我不知道中间数组的初始化大小,因为每个数组级别的初始化大小总是不同的。
它看起来像这样:
char* spawnableVehicles[3][][3] = {
{
{"test1", "test2", "test3"},
{"test1", "test2", "test3"},
{"test1", "test2", "test3"},
{"test1", "test2", "test3"},
{"test1", "test2", "test3"},
},
{
{"test1", "test2", "test3"},
{"test1", "test2", "test3"},
{"test1", "test2", "test3"},
},
{
{"test1", "test2", "test3"},
{"test1", "test2", "test3"},
{"test1", "test2", "test3"},
{"test1", "test2", "test3"},
{"test1", "test2", "test3"},
{"test1", "test2", "test3"},
{"test1", "test2", "test3"},
{"test1", "test2", "test3"},
},
}
如何初始化中间数组的大小?
答案 0 :(得分:2)
您需要清楚地标识所有索引。当我定义 spawnableVehicles [30] [30] [30] 时,定义为 spawnableVehicles [3] [] [3] 会产生错误。我还写了一个代码来检查:
#include<iostream>
using namespace std;
int main(){
char* spawnableVehicles[30][30][30] = {
{
{"test1", "test2", "test3"},
{"test1", "test2", "test3"},
{"test1", "test2", "test3"},
{"test1", "test2", "test3"},
{"test1", "test2", "test3"},
},
{
{"test1", "test2", "test3"},
{"test1", "test2", "test3"},
{"test1", "test2", "test3"},
},
{
{"test1", "test2", "test3"},
{"test1", "test2", "test3"},
{"test1", "test2", "test3"},
{"test1", "test2", "test3"},
{"test1", "test2", "test3"},
{"test1", "test2", "test3"},
{"test1", "test2", "test3"},
{"test1", "test2", "test3"},
},
};
for (int i=0;i<30;i++){
for (int j=0;j<30;j++){
for (int k=0;k<30;k++){
if (spawnableVehicles[i][j][k]!=NULL){
// printing spawnableVehicles[i][j][k] when it has a value other than NULL
cout<<i<<" "<<j<<" "<<k<<" "<<spawnableVehicles[i][j][k]<<"\n";
}
}
}
}
return 0;
}
具有以下结果:
0 0 0 test1
0 0 1 test2
0 0 2 test3
0 1 0 test1
0 1 1 test2
0 1 2 test3
0 2 0 test1
0 2 1 test2
0 2 2 test3
0 3 0 test1
0 3 1 test2
0 3 2 test3
0 4 0 test1
0 4 1 test2
0 4 2 test3
1 0 0 test1
1 0 1 test2
1 0 2 test3
1 1 0 test1
1 1 1 test2
1 1 2 test3
1 2 0 test1
1 2 1 test2
1 2 2 test3
2 0 0 test1
2 0 1 test2
2 0 2 test3
2 1 0 test1
2 1 1 test2
2 1 2 test3
2 2 0 test1
2 2 1 test2
2 2 2 test3
2 3 0 test1
2 3 1 test2
2 3 2 test3
2 4 0 test1
2 4 1 test2
2 4 2 test3
2 5 0 test1
2 5 1 test2
2 5 2 test3
2 6 0 test1
2 6 1 test2
2 6 2 test3
2 7 0 test1
2 7 1 test2
2 7 2 test3
--------------------------------
Process exited after 0.08576 seconds with return value 0
Press any key to continue . . .