Sign in to follow this  
godmodder

boost::multi_array and STL algorithms

Recommended Posts

Hi, Right now I'm iterating over the sectors in my game in the following way: for(boost::multi_array<Sector, 2>::index i = 0; i < Depth; ++i) for(boost::multi_array<Sector, 2>::index j = 0; j < Width; ++j) { Sector *pSector = &m_Sectors[i][j]; pSector->SaveToFile(FilePointer); } But I saw that boost::multi_array also has iterators. So I thought I'd write the following: for_each(m_Sectors.begin(), m_Sectors.end(), bind2nd(mem_fun_ref(&Sector::SaveToFile), FilePointer)); But for some reason this doesn't compile. Anyway, I thought: maybe I'll try to use the iterators manually in a loop and see what it does: typedef boost::multi_array<Sector, 2> SECARRAY; for(SECARRAY::iterator It = m_Sectors.begin(); It != m_Sectors.end(); ++It) g_pLog << "Sector found!" << std::endl; But it only iterates over the elements in one dimension! For example: if I have a 2x1 array it prints out 2 messages correctly, but if I have a 1x2 array it only prints out 1 message! What am I doing wrong here? Am I even supposed to be able to work with the iterators of multi_array? I'd appreciate some help, Jeroen

Share this post


Link to post
Share on other sites
The documentation tells me that the value_type (what you iterate on) is the immediately nested type of the array. Therefore, you're iterating over rows, and need a second iteration to get to the actual values in those rows.

I would suggest using array.origin() and array.origin() + array.num_elements() instead.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this