C++ Boost::多_数组循环
我已经了解了如何使用C++ Boost::多_数组循环,c++,boost,boost-multi-array,C++,Boost,Boost Multi Array,我已经了解了如何使用boost::multi_array::origin()函数在非零基数组上循环的地址,但这只会创建一个循环 如何遍历多维数组的每个维度,例如: for(index i = <origin of dim 1>; ...) { for(index j = <origin of dim 2>; ...) { for(index k = <origin of dim 3>; ...) { myArray[i][j
boost::multi_array::origin()
函数在非零基数组上循环的地址,但这只会创建一个循环
如何遍历多维数组的每个维度,例如:
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>;
}
}
}
for(索引i=;…){
对于(索引j=;…){
对于(索引k=;…){
myArray[i][j][k]=;
}
}
}
当给定一个上下界未知的数组时,成员函数返回一个包含每个维度索引基的容器。shape
成员函数返回一个包含每个维度范围(大小)的容器。您可以使用这两种方法来确定每个维度的索引范围:
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;
}
typedef boost::multi_array_type;
void printary(常量数组类型&a)
{
//查询每个数组维度的范围
索引iMin=a.index_base()[0];
索引iMax=iMin+a.shape()[0]-1;
索引jMin=a.index_base()[1];
索引jMax=jMin+a.shape()[1]-1;
索引kMin=a.索引_基()[2];
索引kMax=kMin+a.shape()[2]-1;
对于(索引i=iMin;i但这要求您在编写代码时知道数组的维数,这将失去多_数组的大部分泛型。@DavidDoria,在A(区段[2][range(1,4)][range(-1,3)]中)
,您可以用任何您喜欢的值替换硬编码的维度值,无论是从磁盘加载的值还是从用户输入的值。为了简化示例,我对数字进行了硬编码。重新排列的示例可以更好地说明如何在数组初始化位置之外查询维度范围。但您仍然是列出了iMin、jMin、kMin…如果printArray
不知道array\u type
是3D的,你会怎么做?