why does creating a 2D array with C++ std::array require an extra pair of {}

Viewed 125

To create a 2D array with std::vector, you'd do

vector<vector<int>> array2d = {{1, 2,  3,  4},
                               {5, 6,  7,  8},
                               {9, 10, 11, 12}};

The outer {} represent the outer vector; the inner {}, inner vector.

However, to create a 2D array with std::array, you'd need to do

array<array<int,4>, 3> array2d = {{{1, 2,  3,  4},
                                   {5, 6,  7,  8},
                                   {9, 10, 11, 12}}};

Why does std::array of std::arrays need an extra pair of enclosing {}?

0 Answers
Related