boost::multi_array and STL algorithms

Started by
1 comment, last by godmodder 15 years, 8 months ago
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[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
Advertisement
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.
Thanks a million, man! That was just what I was looking for :)

This topic is closed to new replies.

Advertisement