I\'m doing a simple normalization on a vector (weights), trying to make use of STL algorithms to make the code as clean as possible (I realize this is pretty trivial with fo
You need to add tot
to the "capture list":
float tot = std::accumulate(weights.begin(), weights.end(), 0);
std::transform(weights.begin(), weights.end(), [tot](float x)->float{return(x/tot);});
Alternatively you can use a capture-default to capture tot
implicitly:
float tot = std::accumulate(weights.begin(), weights.end(), 0);
std::transform(weights.begin(), weights.end(), [=](float x)->float{return(x/tot);});
The lambda can "capture" variables from the ambient scope:
[ ..., N, ... ](int a, int b) -> int { return (a + b) * N; }
^^^^^^^^^^^^^ ^^^^^^^^^^^^ ^^^^
captured vars local params ret.type
You can capture by value or by reference, and you can use the special syntax [=]
and [&]
to capture anything from the ambient scope, i.e. anything you actually end up using.
You need a closure.
float tot = std::accumulate(weights.begin(), weights.end(), 0);
std::transform(weights.begin(), weights.end(), [tot](float x)->float{return(x/tot);});
In this case tot
is captured by value. C++11 lambdas support capturing by:
[x]
[&x]
[&]
[=]
You can mix any of the above in a comma separated list [x, &y]
.