You should capture by reference instead of capturing by value in your lambda:
for_each(range.begin(), range.end(), [&](int i) {
// ^
strings[i] = fizzy(i);
});
It also helps to solve the problem - by default, the call operator of the generated lambda closure is marked as const.
Note:
Another way to make this compiler is to use a keyword mutable, as in the snippet below:
for_each(range.begin(), range.end(), [=](int i) mutable {
// ^^^^^^^
strings[i] = fizzy(i);
});
The keyword mutablediscards constthe generated lambda closure in the call statement.
, , : , , ?
.
UPDATE:
, :
list<int> range(0, 100);
, ( ) 100. , , . (std::iota , ++ 11, ):
#include <algorithm>
list<int> range(100);
iota(begin(range), end(range), 0);