When attempting to initialize a 2D array of type std::array
Consider the following example:
std::array<int, 3> a1 = {{1, 2, 3}}; // Valid initialization of a 1D array
Applying this concept to a 2D array:
std::array<std::array<int, 3>, 2> a2 { {{ {1, 2, 3} }, { {4, 5, 6} }} // Correct initialization };
In this example:
The additional braces in the correct example allow the compiler to distinguish between the class initialization and the initialization of the contained array. This results in proper compilation and initialization of the 2D array.
The above is the detailed content of How to Correctly Initialize a 2D std::array in C ?. For more information, please follow other related articles on the PHP Chinese website!