How can I initialize 2d array with a list of 1d arrays?
void main()
{
int a[] = { 1,2,3 };
int b[] = { 4,5,6 };
int array[][3] = { a,b };
}
std::array
is the way to go here, but if you want to stick to the means of the language (and not the standard lib), and your combined array has the same life time as its constituents, and if the data can be shared, you can have an array of pointers to arrays and say
int a[] { 1,2,3 };
int b[] { 4,5,6 };
decltype(a) *abArr[] {&a, &b};
// equivalent to
// int (*abArr[])[3] { &a, &b };
Note that this is not a 2-dimensional array, and its elements are not integers. But you can still range-loop through both dimensions because the pointers are true pointers to a fixed-size array (as opposed to pointers to a mere int as a result of array decay, which couldn't be range-looped).
Since the array elements are pointers it is necessary to dereference row
:
for (auto const& row : abArr)
for (auto const& e : *row)
cout << e << " ";
Live example: http://coliru.stacked-crooked.com/a/cff8ed0e69ffb436