[英]Boost::multi_array looping
I've looked at this post which addresses how to loop over arrays that aren't zero-based using the boost::multi_array::origin()
function, but that only creates a single loop. 我看过这篇文章 ,讲述了如何使用
boost::multi_array::origin()
函数循环非基于零的数组,但这只会创建一个循环。
How does one traverse each dimension of a multi_array
, for example: 如何遍历
multi_array
每个维度,例如:
for(index i = <origin of dim 1>; ...) {
for(index j = <origin of dim 2>; ...) {
for(index k = <origin of dim 3>; ...) {
myArray[i][j][k] = <something>;
}
}
}
when given an array where both upper and lower bounds are unknown? 给定一个上下界都未知的数组?
The index_bases
member function returns a container with each dimension's index base. index_bases
成员函数返回具有每个维度的索引库的容器。 The shape
member function returns a container with each dimension's extent (size). shape
成员函数返回具有每个维度的范围(大小)的容器。 You can use both of these to determine the range of indices for each dimension: 您可以使用这两个来确定每个维度的索引范围:
typedef boost::multi_array<int, 3> array_type;
void printArray(const array_type& a)
{
// Query extents of each array dimension
index iMin = a.index_bases()[0];
index iMax = iMin + a.shape()[0] - 1;
index jMin = a.index_bases()[1];
index jMax = jMin + a.shape()[1] - 1;
index kMin = a.index_bases()[2];
index kMax = kMin + a.shape()[2] - 1;
for (index i=iMin; i<=iMax; ++i)
{
for (index j=jMin; j<=jMax; ++j)
{
for (index k=kMin; k<=kMax; ++k)
{
std::cout << a[i][j][k] << " ";
}
}
}
}
int main()
{
typedef array_type::extent_range range;
typedef array_type::index index;
array_type::extent_gen extents;
// Extents are hard-coded here, but can come from user/disk.
array_type a(extents[2][range(1,4)][range(-1,3)]);
// Populate array with values...
// Pass the array to a function. The function will query
// the extents of the given array.
print(a);
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.