One-line initialiser for Boost.MultiArray

后端 未结 3 633
被撕碎了的回忆
被撕碎了的回忆 2020-12-31 20:53

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

        
3条回答
  •  伪装坚强ぢ
    2020-12-31 21:22

    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 arr(std::vector(n,size));
    

    This initializes a n-dimensional multi_array with each dimension's size set to size.

提交回复
热议问题