The problem
I have an array of int
's which vary in value called sizes
. I want to create a 2d array (called arr
) where the first dimension size is the length of the sizes
array, and the second dimension's size is the value of the integer at that index. I hope the following code makes it easier to understand my question.
int sizes[4] = { 1, 2, 3, 4 };
// What I need is an array with this shape
{
{0}, // arr[0]
{ 0, 0 }, // arr[1]
{ 0, 0, 0 }, // arr[2]
{ 0, 0, 0, 0 } // arr[3]
};
My actual code, the sizes array is larger, so I would like to avoid lots of repeating lines of code if that is possible.
My attempts
I am new to C++
, but I have read about constexpr
. I think I can create a function to return this array, but I have yet to get this to work.
I also think I can loop over the sizes
array, and for each int
, create an array with that size, but I can't then assign it to arr
.