Boost logo

Boost Users :

From: Matt Hurd (matt_at_[hidden])
Date: 2003-07-04 11:09:57


Not sure how to force a for_each with a lambda to invoke the random
function object successively. In the for_each version below get_long is
called just once. Not sure how to use var, std_function, bind or
something else to overcome this.

I'm sure the answer is straight forward... anyone?

Regards,

Matt.

#include <iostream>
#include <boost/random.hpp>
#include <vector>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>

typedef boost::mt19937 base_generator_type;
base_generator_type generator( 100123u );
boost::uniform_int<base_generator_type,long>
gen_long(generator,-100,100);

using namespace std;
using namespace boost::lambda;

int main(int argc, char *argv[])
{
  vector<long> v(10);
  for_each(v.begin(),v.end(), _1 = gen_long() );

  for_each(v.begin(), v.end(), cout << _1 << ' ');
  cout << "\n";
  
  for( vector<>::iterator i = v.begin(); i != v.end(); ++i )
  {
    *i = gen_long();
  }
  
  for_each(v.begin(), v.end(), cout << _1 << ' ');
  cout << "\n";
  
  system("PAUSE");
  return 0;
}


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