Boost::multi_array looping
Boost::multi_array looping
我看过这篇文章,它介绍了如何使用boost::multi_array::origin()
函数在非零基数组上循环,但只创建一个循环。
如何遍历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>;
}
}
}
当给定一个上界和下界都未知的数组时?
index_bases
成员函数返回一个包含每个维度的索引基的容器。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;
}
相关文章:
- C++11 中不同类型的对象的 std::array 的替代方案
- constexpr begin of a std::array
- C++如果必须在编译时确定大小,std::array 有什么意义?
- OpenGL VBO Indexing ( How to compute Index Array)
- 标准::unordered_map 中的 std::array 的值初始化
- "Warning: Comma within array index expression"但逗号分隔函数参数
- 确保编译时的特定 std::array 位置
- std::array的长度有大小限制吗?
- 将 std::array 移动到另一个 std::array
- 首先按给定顺序打印所有数字,然后使用 Array 打印所有字符和其他符号
- 为什么 std::shared_ptr 被认为是"heavy"和"expensive",但 std::array "same perfprmance as plain (c-style) arrays
- 将 **float array 从 C++ Dll 传递给 python
- std::bind on statd::array 的运算符 []
- 检查输入 std::array 指针数据是否等于某个常量数组
- 我可以安全地复制矢量<array>吗?
- 解析问题 - 预期的非限定 ID - #include <array> 编译错误
- 如何读/写或遍历 std::array 中的特定元素范围?
- 通过 host() 从 af::array 检索数据会导致错误的数据
- 是否可以使用 std::array 作为 POD 结构的数据容器?
- 如何在C++中传递一个大小未知的 std::array?