使用多个std :: arrays初始化2D std :: array

时间:2017-12-13 03:33:06

标签: c++

我正在尝试使用C ++中的数组初始化2D std :: array:

const array<bool, 7> LEDZERO = { 1,1,1,0,1,1,1 };
const array<bool, 7> LEDONE[] = { 0,0,1,0,0,1,0 };
const array<bool, 7> LEDTWO[] = { 1,0,1,1,1,0,1 };
const array<bool, 7> LEDTHREE[] = { 1,0,1,1,0,1,1 };
const array<bool, 7> LEDFOUR[] = { 0,1,1,1,0,1,0 };
const array<bool, 7> LEDFIVE[] = { 1,1,0,1,0,1,1 };
const array<bool, 7> LEDSIX[] = { 1,1,0,1,1,1,1 };
const array<bool, 7> LEDSEVEN[] = { 1,0,1,0,0,1,0 };
const array<bool, 7> LEDEIGHT[] = { 1,1,1,1,1,1,1 };
const array<bool, 7> LEDNINE[] = { 1,1,1,1,0,1,0 };
const array<array<bool, 7>, 10> LEDS = { { {LEDZERO}, {LEDONE}, {LEDTWO},     
{LEDTHREE}, {LEDFOUR}, {LEDFIVE}, {LEDSIX}, {LEDSEVEN}, {LEDEIGHT}, 
{LEDNINE} } };

只有第一个LEDZERO正好设置为LEDS [0],LEDS [1-9]错误。

1 个答案:

答案 0 :(得分:2)

在大多数[]变量后,您有假LED个。这将创建一个std::array的C风格数组(可能不是你想要的)。

#include <array>

int main()
{
    const std::array<bool, 7> LEDZERO = {{ 1,1,1,0,1,1,1 }};
    const std::array<bool, 7> LEDONE = {{ 0,0,1,0,0,1,0 }};
    const std::array<bool, 7> LEDTWO = {{ 1,0,1,1,1,0,1 }};
    const std::array<bool, 7> LEDTHREE = {{ 1,0,1,1,0,1,1 }};
    const std::array<bool, 7> LEDFOUR = {{ 0,1,1,1,0,1,0 }};
    const std::array<bool, 7> LEDFIVE = {{ 1,1,0,1,0,1,1 }};
    const std::array<bool, 7> LEDSIX = {{ 1,1,0,1,1,1,1 }};
    const std::array<bool, 7> LEDSEVEN = {{ 1,0,1,0,0,1,0 }};
    const std::array<bool, 7> LEDEIGHT = {{ 1,1,1,1,1,1,1 }};
    const std::array<bool, 7> LEDNINE = {{ 1,1,1,1,0,1,0 }};
    const std::array<std::array<bool, 7>, 10> LEDS = {{
            {LEDZERO},
            {LEDONE},
            {LEDTWO},     
            {LEDTHREE},
            {LEDFOUR},
            {LEDFIVE},
            {LEDSIX},
            {LEDSEVEN},
            {LEDEIGHT}, 
            {LEDNINE}
        }};
}

如果您不需要LEDZEROLEDNINE,则可以将其写得更紧凑。

#include <array>

int main()
{
    const std::array<std::array<bool, 7>, 10> LEDS = {{
            {{ 1,1,1,0,1,1,1 }},
            {{ 0,0,1,0,0,1,0 }},
            {{ 1,0,1,1,1,0,1 }},
            {{ 1,0,1,1,0,1,1 }},
            {{ 0,1,1,1,0,1,0 }},
            {{ 1,1,0,1,0,1,1 }},
            {{ 1,1,0,1,1,1,1 }},
            {{ 1,0,1,0,0,1,0 }},
            {{ 1,1,1,1,1,1,1 }},
            {{ 1,1,1,1,0,1,0 }}
        }};
}