如何动态设置Boost Multi_Array的维数 [英] How to dynamically set dimensionality for a boost multi_array
问题描述
我有一个函数,需要创建一个增强的multi_array,其大小不是先验的.但是,对于任何给定的维度,范围都是已知的.如何为数组增量建立扩展对象?
I have a function that needs to create a boost multi_array for which the dimensions are not known a priori. However, the extents ARE known for any given dimensionality. How do I incrementally build up an extents object for my array?
类似的东西:
array_type::extent_gen<array_type::dimensionality> my_extents;
for (size_type d = 0; d < array_type::dimensionality; d++) {
extents = extents[5];
}
array_type my_array(extents);
将设置一个尚未确定的数组,使每个维度的范围都为5 ....
Would set an not-yet-determined array to have an extent of 5 in every dimension....
推荐答案
我不认为可以,因为multi_array
的维度是在类型的模板参数中编码的:
I don't think you can, since the dimensionality of multi_array
is encoded in the type's template arguments:
typedef boost::multi_array<double, 3> array_type;
从根本上不同于
typedef boost::multi_array<double, 2> array_type;
您可能要做的最好的事情"是
The "best" you might do is have
static const int MAX_DIMENSIONS = 10;
typedef boost::multi_array<double, MAX_DIMENSIONS> array_type;
然后构建范围以将未使用"维的范围保留在1
:
And then build the extents to leave the "unused" dimension's extent at 1
:
#include <boost/multi_array.hpp>
#include <iostream>
static const int MAX_DIMENSIONS = 5;
typedef boost::multi_array<double, MAX_DIMENSIONS> array_type;
array_type make_array(int dims, int default_extent = 5) {
boost::array<int, MAX_DIMENSIONS> exts = {};
std::fill_n(exts.begin(), exts.size(), 1);
std::fill_n(exts.begin(), dims, default_extent);
return array_type(exts);
}
int main() {
auto a = make_array(3);
auto b = make_array(2);
std::copy(a.shape(), a.shape() + a.dimensionality, std::ostream_iterator<size_t>(std::cout << "\na: ", " "));
std::copy(b.shape(), b.shape() + b.dimensionality, std::ostream_iterator<size_t>(std::cout << "\nb: ", " "));
}
打印
a: 5 5 5 1 1
b: 5 5 1 1 1
这篇关于如何动态设置Boost Multi_Array的维数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!