简单的3D阵列C ++ [英] Simple 3D Array C++
问题描述
我是C ++的新手,我试图创建一个简单的静态3维数组,然后在控制台中将其打印出来.
I am a novice in C++ and I am trying to create a simple static 3 Dimensional Array and then print it out in console.
这是我当前的代码:
#include <iostream>
using namespace std;
int main()
{
const int MAX_ROW = 2;
const int MAX_COL = 2;
const int MAX_HEIGHT = 2;
int MyArray[MAX_ROW][MAX_COL][MAX_HEIGHT] = { {1,1},
{2,10},
{3,15},
{4,20},
{5,25},
{6,30},
{7,35},
{8,40} };
for(int Row = 0; Row < MAX_ROW; ++Row)
{
for(int Col =0; Col < MAX_COL; ++Col)
{
for(int Height = 0; Height < MAX_HEIGHT; ++Height)
{
cout << "Integer["<< Row << "][" << Col << "][" << Height << "] = " << MyArray[MAX_ROW][MAX_COL][MAX_HEIGHT] << endl;
}
}
}
return 0;
}
编译时,编译器会通知我"error: too many initializers for ‘int [2][2][2]"
When I compile the compiler notifies me stating "error: too many initializers for ‘int [2][2][2]"
其他问题使用了我不熟悉的指针.
提前谢谢!
语法是错误的,因此我已使用正确的相应代码对它进行了纠正,如下所示.现在,在程序的输出中,每个数组空间为32767.是一个完整的整数空间,而不是分配的值.有人可以在回答中解决这个问题吗?除了数组的初始化之外,我没有更改任何代码.
The syntax is wrong so I have corrected it with the correct corresponding code as answered below. Now in the output of the program each array space is 32767. A full integer space instead of the assigned values. Can anybody address this in their answer? I have not changed any code except my initialisation of the array.
推荐答案
按照以下说明更改代码.您会看到有两个包含两个元组的组,每个元组中都有两个元素.
Change the code as per following. You can see there are 2 groups containing two tuples each having two elements in it.
int MyArray[MAX_ROW][MAX_COL][MAX_HEIGHT] = {
{ {1,1},{2,10} },
{ {4,20},{5,25} }
};
请看下面的示例,以使其更清楚
Have a look in following example to make it more clear
int arr[2][3][4] = {
{ {1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4} },
{ {1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4} }
};
如您所见,有两组,每组包含3组,每组4个数字.
As you can see, there are two groups, each containing three groups of 4 numbers.
这篇关于简单的3D阵列C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!