简体   繁体   English

在C ++中定义2D数组

[英]Defining 2D arrays in C++

int train [4] [3] = {   0, 0, 0,
                                      0, 1, 0,
                                      1, 0, 0,
                                      1, 1, 1 };

Is that a valid initialization of a 2d array in C++ 这是C ++中2D数组的有效初始化吗?

And the rows will be 0,0,0 (row 1), (0,1,0) (row2), (1,0,0) (row3) and (1,1,1) (row 4) ? 并且行将是0,0,0(第1行),(0,1,0)(第2行),(1,0,0)(第3行)和(1,1,1)(第4行)?

And is it equivalent to 它等于

 int train [4] [3] = {{0, 0, 0},
                       {0, 1, 0},
                       {1, 0, 0},
                       {1, 1, 1}};
int train [4] [3] = {   0, 0, 0,
                        0, 1, 0,
                        1, 0, 0,
                        1, 1, 1 };

is a valid initialization of a 2D array in C++. 是C ++中2D数组的有效初始化。

From the C++11 Standard: 从C ++ 11标准:

8.5.1 Aggregates 8.5.1骨料

10 When initializing a multi-dimensional array, the initializer-clauses initialize the elements with the last (right-most) index of the array varying the fastest (8.3.4). 10初始化多维数组时,初始化子句使用数组的最后一个(最右)索引以最快的速度(8.3.4)初始化元素。 [ Example: [ 示例:

 int x[2][2] = { 3, 1, 4, 2 }; 

initializes x[0][0] to 3 , x[0][1] to 1 , x[1][0] to 4 , and x[1][1] to 2 . x[0][0]初始化为3 ,将x[0][1]初始化为1 ,将x[1][0]初始化为4 ,将x[1][1]初始化为2 On the other hand, 另一方面,

 float y[4][3] = { { 1 }, { 2 }, { 3 }, { 4 } }; 

initializes the first column of y (regarded as a two-dimensional array) and leaves the rest zero. 初始化y的第一列(视为二维数组),其余的保持零。 end example ] 结束示例 ]

Yes! 是! It is a valid intialization in c++. 这是c ++中的有效初始化。

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

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