On Fri, Aug 14, 2009 at 11:39 AM, HweeKuan Lee <hweekuan@yahoo.com> wrote:
i'm new to boost, but use the standard template library a lot. I have
problems with the following code.

line 12: according to the reference, i should be able to do this, but cannot compile. compile error is very long, tell me if I should include them here.

my compiler: gcc version 4.0.1 (Apple Inc. build 5490)

thanks in advance for your help.

 1 #include <boost/multi_array.hpp>
 2
 3 int main ( ) {
 4
 5   typedef boost::multi_array<double,2> matrix_type;
 6   typedef matrix_type::iterator        iterator;
 7
 8   matrix_type mat(boost::extents[2][3]);
 9   double v = .2;
 10
 11   for(iterator it=mat.begin();it!=mat.end();++it) {
 12     (*it) = v; // compile error
 13   }
 14 }

Your iterator dereferences to a slice through your 2-d array. To initialise all
the 6 double elements of mat you need to nest another loop.

#include <boost/multi_array.hpp>

int main ( )
{

  typedef boost::multi_array<double,2> matrix_type;
  typedef matrix_type::iterator        iterator;

  matrix_type mat(boost::extents[2][3]);
  double v = .2;

  for(iterator it=mat.begin();it!=mat.end();++it)
  {
    for(int i=0;i!=3;++i)
      (*it)[i] = v;
  }
}

- Rob.