Boost lambda will evaluate rand
before the functor is made. You need to bind
it, so it's evaluated at lambda evaluation time:
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp> // for bind
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <vector>
int main(void)
{
namespace bl = boost::lambda;
typedef std::vector<int> int_vec;
static const size_t MaxListSize = 10;
static const int MaxSize = 20;
int_vec theList;
theList.resize(MaxListSize);
std::srand(static_cast<unsigned>(std::time(0)));
std::for_each(theList.begin(), theList.end(),
bl::_1 = bl::bind(std::rand) % MaxSize);
std::for_each(theList.begin(), theList.end(), std::cout << bl::_1 << ' ' );
}
This works as expected. Note that for_each
is suppose to be non-mutating. In your specific usage, you would want transform
(to "transform" the range from "0,0,0,..." to your values):
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <vector>
int main(void)
{
namespace bl = boost::lambda;
typedef std::vector<int> int_vec;
static const size_t MaxListSize = 10;
static const int MaxSize = 20;
int_vec theList;
theList.resize(MaxListSize);
std::srand(static_cast<unsigned>(std::time(0)));
std::transform(theList.begin(), theList.end(), theList.begin(),
bl::_1 = bl::bind(std::rand) % MaxSize);
std::for_each(theList.begin(), theList.end(), std::cout << bl::_1 << ' ' );
}
However, the correct solution is to use generate_n
. Why make a bunch of 0's just to overwrite them?
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <vector>
int main(void)
{
namespace bl = boost::lambda;
typedef std::vector<int> int_vec;
static const size_t MaxListSize = 10;
static const int MaxSize = 20;
int_vec theList;
theList.reserve(MaxListSize); // note, reserve
std::srand(static_cast<unsigned>(std::time(0)));
std::generate_n(std::back_inserter(theList), MaxListSize,
bl::bind(std::rand) % MaxSize);
std::for_each(theList.begin(), theList.end(), std::cout << bl::_1 << ' ' );
}