Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usage of boost lambdas

I am new to boost and trying to write some simple programs to understand it. Here in the following piece of code I am trying to fill an array with random numbers. Here is my code:

    using namespace boost::lambda;
    srand(time(NULL));
    boost::array<int,100> a;
    std::for_each(a.begin(), a.end(), _1=rand());

But it looks like rand() is getting evaluated only once and my array is containing the same values for every element. Can anybody point what is wrong with this code?

like image 215
Naveen Avatar asked Nov 18 '25 21:11

Naveen


1 Answers

Seems like you need to use delayed function call

std::for_each(a.begin(), a.end(), boost::lambda::_1= boost::lambda::bind(rand) );

Here is another interesting situation: Delaying constants and variables


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!