On Nov 27, 2006, at 1:14 AM, Haroon Khan wrote:
Hi,
I'm experimenting with the boost lambda library, and I wrote the
following code, to go through the vector of numbers and print out
statements if the number is greater than 5 or less than 5
int a[]={1,2,3,4,5,6,7,8,9,10,10,9,8,4,7,6,5,4,3,3,1};
std::vector<int> v(a,a+sizeof(a)/sizeof(int));
std::for_each
( v.begin()
, v.end()
, (if_(_1<5)[
std::cout<
The boost::basic_format::operator % code gives an
assertion since
the object that is being passed is the placeholder object, and not
the current value. Is there a correct way of doing this?
There is, but it is kind of complicated.
Lambda Library gets kind of hairy with more complex lambda expressions:
#include
#include
#include
#include
#include
#include <iostream>
using namespace boost::lambda;
int main() {
int a[]={1,2,3,4,5,6,7,8,9,10,10,9,8,4,7,6,5,4,3,3,1};
std::vector<int> v(a,a+sizeof(a)/sizeof(int));
std::for_each
( v.begin(), v.end(),
(if_(_1<5)[
std::cout <<
retboost::format(bind(constructorboost::format(), "%d is
less than 5\n") % _1)
].else_[
std::cout <<
retboost::format(bind(constructorboost::format(), "%d is
greater than 5\n") % _1)
]) );
}
The bind(constructorboost::format(), ...) is necessary, because you
really need to construct a new
format object at every iteration, otherwise the format library throws
an exception saying you
are trying to pass to many parameters to a format object, more that
what is specfied in the format string.
the retboost::format(...) is needed to tell lambda what the return
type of format's operator% is.
All in all, the above lambda is complex enough that writing a
function object explicitly might be
more justified... at least until we have built-in lambdas, if that
ever happens.
Cheers,
Jaakko
Thanks, Haroon
_______________________________________________
Boost-users mailing list
Boost-users@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users