how to traverse a boost::multi_array(如何遍历 boost::multi_array)
问题描述
我一直在研究 boost::multi_array 库,以寻找允许您在单个 for 循环中遍历整个 multi_array 的迭代器.
I have been looking into the boost::multi_array library in search of an iterator that allows you to traverse the whole multi_array in a single for loop.
我认为那个库中没有这样的迭代器.(在那里找到的迭代器可以让你遍历 multi_array 的一个维度)
I don't think there is any such iterator in that library. (The iterators that are found there let you traverse a single dimension of the multi_array)
我错了吗?
如果没有,是否有任何库定义了这样的迭代器?
Am I wrong?
If not, is there any library that defines such an iterator?
进入细节,我想写一些类似的东西:
Entering into details, I'd like to write something like:
boost::multi_array< double, 3 > ma(boost::extents[3][4][2]);
for( my_iterator it = ma.begin(); it != ma.end(); ++it )
{
// do something
// here *it has element type (in this case double)
}
并获得重复 3x4x2 次的循环
and obtain a loop that repeats 3x4x2 times
推荐答案
您可以使用 中的 std::for_each 实现来访问每个人元素.Boost 文档
You can use an implementation of std::for_each from <algorithm> to access each individual element. There is an example in the Boost documentation
或者,您可以使用 array::origin() 和 array::num_elements() 如下:
Alternatively, you can use array::origin() and array::num_elements() as follows:
boost::multi_array< double, 3 > ma(boost::extents[3][4][2]);
for(auto i = ma.origin(); i < (ma.origin() + ma.num_elements()); ++i)
{
// do something with i
}
这篇关于如何遍历 boost::multi_array的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何遍历 boost::multi_array
基础教程推荐
- 初始化列表*参数*评估顺序 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- CString 到 char* 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
