I have a n-dimensional Boost.MultiArray I initialize as follows:
const int n=3, size=4; //# of dimensions and size of one dimension
boost::multi_array<char,n> arr;
boost::array<size_t,n> extents; //size of each dimension
extents.assign(size); //assign size to each dimension -> {{4, 4, 4}}
arr.resize(extents);
So I have 4 lines of code to get the MultiArray, but I'd like to do it in one line.
Is there any simple way to generate an MultiArray with n dimensions each having size
length (so I can write arr(samevaluearray(n,size))
) or did I miss a handy constructor for MultiArray?
Edit: It should work without depending on a certain value of n, i.e. arr({{size,size}}
would only work for n=2
.
Since it may not be clear: boost::multi_array<char,n>(boost::extents[4][4][4])
correctly initializes a 4x4x4-array, but every time n
is changed in the sourcecode, every initialization has to be updated by hand, so it's not an option.
Turns out, std::vector
has a constructor, that constructs a vector with a constant value repeated n times, so a possible solution looks like this:
const int n=2, size=4; //# of dimensions and size of one dimension
boost::multi_array<char,n> arr(std::vector<size_t>(n,size));
This initializes a n-dimensional multi_array with each dimension's size set to size.