问题描述
我有一个n维Boost.MultiArray的我初始化如下:
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);
所以我有4行code,以获得MultiArray的,但我想这样做在同一行。
是否有产生的MultiArray与n维各有尺寸
长度的任何简单的方式(这样我就可以写 ARR(samevaluearray(N,大小))
)或我错过了一个方便的构造MultiArray的?
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?
编辑:的应该不依赖于n的一定价值的工作,即 ARR({{大小,大小}}
只会工作为 N = 2
。
It should work without depending on a certain value of n, i.e. arr({{size,size}}
would only work for n=2
.
由于它可能不是很清楚:的boost :: multi_array的&LT;焦炭,N&GT;(升压::程度[4] [4] [4])
正确初始化型4x4x4阵列,但每次 N
源$ C $ C变更,每次初始化需要手工进行更新,所以它不是一个选项。
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.
推荐答案
原来,的std ::矢量
有一个构造函数,即构造一个向量重复一个恒定值n次,所以一个可能的解决方案是这样的:
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));
这与初始化每个维度的大小设置为大小N维的multi_array。
This initializes a n-dimensional multi_array with each dimension's size set to size.
这篇关于单行初始化器的Boost.MultiArray的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!