On
9/27/05, Stuart Dootson <stuart.dootson@gmail.com> wrote:
That's
not going to work because the operator() call in the loop (i.e.
m(...))
is evaluated when the lambda expression is created, not each
time the
lambda expression is evaluated.
Try replacing
var( m( c / m.w,
c % m.w ) )
with
bind(&MyMatrix::operator(), var(m),
var(c) / m->*&MyMatrix::w, var(c)
%
m->*&MyMatrix::w)
This makes all accesses to m & c lazy (I
think), so should update m as you want.
Ok,
that fixes it. Now, hardcore algorithmism aside, I don't see any advantage in
this over the
for loop: it's slower, and can hardly be called an
improvement in readability. So I think I'll stick to
the old-fashioned
syntax in this case. Still, interesting to know that boost::lambda actually
can do
this.
Using std::copy and a function_output_iterator from
the iterator adaptor library would IMHO, be more readable and
resuable.
#include
<boost/function_output_iterator.hpp>
struct matrix_filler
{
matrix_filler( matrix& m ): m(m), counter( 0 ) {}
void operator()( int val
)const
{
m(
counter / m.w , counter % m.w ) = val;
++counter
}
matrix&
m;
long
counter;
};
std::copy( v.begin(), v.end(),
boost::make_function_output_iterator(matrix_filler(m)) );
Or provide a "linear" iterator to your matrix
class.
Jeff Flinn