I don't understand why my commented lambda expression doesn't work as expected (when uncommented, of course :-)). The function returns a vector<int> of random numbers and the offending statement is supposed to scale each of the initial random values in the vector to the range (1, die).
Is it because the for_each is passing an iterator to the lambda?
vector<int> RMRandom::Dice(unsigned quantity, unsigned die)
{
vector<int> results(quantity, 0);
generate(results.begin(), results.end(), rand);
for_each(results.cbegin(), results.cend(),
[](constint element) {cout << element << " / "; });
cout << endl;
// I don't understand why this lambda expression doesn't modify the vector
// for_each(results.begin(), results.end(),
// [](int& element) {return element * 100 / RAND_MAX; });
// instead I have to do it the old-fashioned way
// Iterate through the vector and normalize the random numbers to (1, 100]
for (auto element = 0; element != results.size(); element++)
{
results[element] = results[element] * 100 / RAND_MAX;
}
// for (auto element : results)
// results[element] = results[element] * 100 / RAND_MAX;
return results;
}
I also believe that's the fundamental problem with the for-range statement at the end.
There certainly may be other problems and better implementations. I appreciate comments and advice but my first issue is understanding the use of lambda expressions in for_each statements with the STL.
You don't modify element inside the lambda and for_each doesn't care about a return value of the function it calls.
...but a std::transform does. Ok, it seems so clear now you put it that way :-). I think I understand how using for_each with a function object should work (because it would return void) but the lambda had me confused, although it, too, should return the equivalent of void (or whatever it does). So, for a for_each, it should be: