简体   繁体   English

在C ++中,如何编写三维数组?

[英]In C++, how do I write a 3-dimensional array?

Thanks for looking! 谢谢你的期待!

Background 背景

I am building a quadcopter and am placing on each of it's four arms a strip of RGB LEDs which are individually addressable. 我正在构建一个四轴飞行器,并在它的四个臂上放置一条可以单独寻址的RGB LED条。 There are 6 LEDs per strip. 每个条带有6个LED。

Further, there are six steps in a sequence to how I wish to toggle the lights so all six lights on the strip don't necessarily toggle at once. 此外,按顺序有六个步骤,我希望如何切换灯光,这样条带上的所有六个灯都不一定要切换。

In pseudo code, here is what I am trying to create: 在伪代码中,这是我想要创建的:

someArray = A group of 4 LED strips {Strip 1: [
    Step 1: [ LED 1: [R, G, B], . . ., LED 6: [R, G, B] ],
    . . .,
    Step 6: [ LED 1: [R, G, B], . . ., LED 6: [R, G, B] ],
],
. . .,
Strip 4: [
    Step 1: [ LED 1: [R, G, B], . . ., LED 6: [R, G, B] ],
    . . .,
    Step 6: [ LED 1: [R, G, B], . . ., LED 6: [R, G, B] ],
]};

I am using an Arduino to drive the lights, and the code is in C++ with which I am not very familiar. 我正在使用Arduino来驱动灯光,代码是用C ++编写的,我不太熟悉。

What I have tried so far: 到目前为止我尝试了什么:

Here is the code I have tried so far, but when I compile in the Arduino IDE, I get an error that says "Braces around scalar initializer for type 'int'". 这是我到目前为止尝试过的代码,但是当我在Arduino IDE中编译时,我收到一条错误,上面写着“对于'int'类型的标量初始化器的大括号”。

int gpsHoldArr[4][6][6] = {
  {
    {{255,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {255,0,0}, {0,0,0}, {0,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {0,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}}
},
  {
    {{255,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {255,0,0}, {0,0,0}, {0,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {0,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}}
},
  {
    {{255,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {255,0,0}, {0,0,0}, {0,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {0,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}}
},
  {
    {{255,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {255,0,0}, {0,0,0}, {0,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {0,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}}
},
};

What am I missing here? 我在这里错过了什么?

Intended Usage 预期用途

Intended use would be something like this: 预期用途将是这样的:

//iterate each of the 6 sets of RGB values and assign them to the correct (sequential) LED
void toggleLights(int lights[]){
  for(int i = 0; i <= 6; ++i) 
  {
    set_color_led(i, lights[i][0], lights[i][1], lights[i][2]);
  } 
}

toggleLights(gpsHoldArr[1][0]);//Get the first step of the second arm.

Thanks in advance. 提前致谢。

You wrote the initialization for a 4d array as RGB has 3 components. 您为4d数组编写了初始化,因为RGB有3个组件。 You probably want to do something like this: 你可能想做这样的事情:

struct rgb {
  int r, g, b;
};

rgb gpsHoldArr[4][6][6] = {
  {
    {{255,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {255,0,0}, {0,0,0}, {0,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {0,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}}
},
  {
    {{255,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {255,0,0}, {0,0,0}, {0,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {0,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}}
},
  {
    {{255,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {255,0,0}, {0,0,0}, {0,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {0,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}}
},
  {
    {{255,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {255,0,0}, {0,0,0}, {0,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {0,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}}
},
};

Or, change your declaration to this: 或者,将您的声明更改为:

int gpsHoldArr[4][6][6][3]

Use a byte and cut the memory usage in half 使用一个字节并将内存使用量减半

typedef struct rgb_t {
  byte r;
  byte g;
  byte b;
};

rgb_t gpsHoldArr[4][6][6] = {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM