[英]how to traverse a boost::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. 我一直在研究boost :: multi_array库,寻找一个迭代器,它允许你在一个for循环中遍历整个 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)
(在那里找到的迭代器允许您遍历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)
}
and obtain a loop that repeats 3x4x2 times 并获得一个重复3x4x2次的循环
You can use an implementation of std::for_each
from <algorithm>
to access each individual element. 您可以使用
<algorithm>
的std::for_each
<algorithm>
来访问每个单独的元素。 There is an example in the Boost documentation Boost文档中有一个例子
Alternatively, you can use array::origin()
and array::num_elements()
as follows: 或者,您可以使用
array::origin()
和array::num_elements()
,如下所示:
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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.