Boost logo

Boost Users :

Subject: Re: [Boost-users] iterators and value_type in multi_array
From: Robert Jones (robertgbjones_at_[hidden])
Date: 2009-08-14 15:03:38


On Fri, Aug 14, 2009 at 11:39 AM, HweeKuan Lee <hweekuan_at_[hidden]> 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.



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net