简单的 3D 数组 C++

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19289878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:40:31  来源:igfitidea点击:

Simple 3D Array C++

c++arrays

提问by JOG-Design

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.

我是 C++ 的新手,我正在尝试创建一个简单的静态 3 维数组,然后在控制台中将其打印出来。

Here is my current code:

这是我当前的代码:

#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;
}

When I compile the compiler notifies me stating "error: too many initializers for ‘int [2][2][2]"

当我编译时,编译器通知我说明 "error: too many initializers for ‘int [2][2][2]"

Other questions have used pointers which I am not familiar with.

其他问题使用了我不熟悉的指针。

Thank you in advance!

先感谢您!

Edit: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.

编辑:语法错误,所以我用正确的相应代码更正了它,如下所述。现在在程序的输出中,每个数组空间都是 32767。一个完整的整数空间而不是分配的值。任何人都可以在他们的回答中解决这个问题吗?除了数组的初始化之外,我没有更改任何代码。

采纳答案by Vallabh Patade

Change the code as per following. You can see there are 2 groups containing two tuples each having two elements in it.

更改代码如下。您可以看到有 2 个组包含两个元组,每个组中有两个元素。

 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} } 
                     };

As you can see, there are two groups, each containing three groups of 4 numbers.

如您所见,有两组,每组包含三组,每组 4 个数字。

回答by Jonathon Reinhart

Your syntax is wrong.

你的语法是错误的。

int a[2][2][3] = {     // Initialize entire variable
  {                    //   1 of 2 (leftmost array)
    { 1, 2, 3 },       //     1 of 2 (inner array)
    { 4, 5, 6 },       //     2 of 2 (inner array)
  },

  {                    // 2 of 2 (leftmost array)
    { 7, 8, 9 },       //     1 of 2 (inner array)
    { 10, 11, 12 },    //     2 of 2 (inner array)
  },
}

What you've shown would be an int [8][2].

你所展示的将是一个int [8][2].

回答by Oragon Efreet

Your declare a 2x2x2 array, but defining it as a 2x8 array.

您声明了一个 2x2x2 数组,但将其定义为一个 2x8 数组。

Also, when you print the content of your array, you use MAX_* as indexes instead of your loop variables.

此外,当您打印数组的内容时,您使用 MAX_* 作为索引而不是循环变量。

#include <iostream>

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}, {1,-1}
        },
        {
            {2,10}, {2,-10}
        }
    };

    for(int Row = 0; Row < MAX_ROW; ++Row)
        for(int Col =0; Col < MAX_COL; ++Col)
            for(int Height = 0; Height < MAX_HEIGHT; ++Height)
                std::cout << "Integer["<< Row << "][" << Col << "][" << Height << "] = " << MyArray[MAX_ROW][MAX_COL][MAX_HEIGHT] << std::endl;


  return 0;
}

回答by micheal.yxd

you array MyArray[MAX_ROW][MAX_COL][MAX_HEIGHT]only can hold 2*2*2=8 elements, but

你的数组MyArray[MAX_ROW][MAX_COL][MAX_HEIGHT]只能容纳 2*2*2=8 个元素,但是

{ {1,1},
                           {2,10},
                           {3,15},
                           {4,20},
                           {5,25},
                           {6,30},
                           {7,35},
                           {8,40} };

has 16 elements. so there are too many initializers

有 16 个元素。所以初始化器太多了

回答by Sopel

Apart from wrong array initialization as others have pointed out you also have an error in printing. You always print the same element that doesn't even exist (which is undefined behaviour).

除了其他人指出的错误数组初始化之外,您在打印时也有错误。您总是打印甚至不存在的相同元素(这是未定义的行为)。

cout << "Integer["<< Row << "][" << Col << "][" << Height << "] = " << MyArray[Row][Col][Height] << endl;