Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

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)  
}  

and obtain a loop that repeats 3x4x2 times

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
155 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can use an implementation of std::for_each from <algorithm> to access each individual element. There is an example in the Boost documentation

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
}  

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...