Say I've got an N-dimensional boost::multi_array (of type int for simplicity), where N
is known at compile time but can vary (i.e. is a non-type template parameter). Let's assume that all dimensions have equal size m
.
typedef boost::multi_array<int, N> tDataArray;
boost::array<tDataArray::index, N> shape;
shape.fill(m);
tDataArray A(shape);
Now I would like to loop over all entries in A
, e.g. to print them. If N was 2 for example I think I would write something like this
boost::array<tDataArray::index, 2> index;
for ( int i = 0; i < m; i++ )
{
for ( int j = 0; j < m; j++ )
{
index = {{ i, j }};
cout << A ( index ) << endl;
}
}
I've used an index object to access the elements as I think this is more flexible than the []-operator here.
But how could I write this without knowing the number of dimensions N
. Is there any built-in way? The documentation of multi_array is not very clear on which types of iterators exist, etc.
Or would I have to resort to some custom method with custom pointers, computing indices from the pointers, etc.? If so - any suggestions how such an algorithm could look like?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…