I’m trying to initialize a list to random integers using a for_each and a lambda function. I’m new to boost.lambda functions so I may be using this incorrectly but the following code is producing a list of the same numbers. Every time I run it the number is different but everything in the list is the same:
srand(time(0));
theList.resize(MaxListSize);
for_each(theList.begin(), theList.end(), _1 = (rand() % MaxSize));
Boost lambda will evaluate
randbefore the functor is made. You need tobindit, so it’s evaluated at lambda evaluation time:This works as expected.
However, the correct solution is to use
generate_n. Why make a bunch of 0’s just to overwrite them?