如何遍历一个boost :: multi_array的 [英] how to traverse a boost::multi_array
问题描述
我一直在寻找到了boost :: multi_array的图书馆寻找一个迭代器,允许你遍历的全的的multi_array在一个for循环。
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的
的实施从&LT;算法&GT ;
来访问每个元素。有一个在 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
另外,你可以使用阵列::原点()
和阵列:: 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的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!