【发布时间】:2011-10-29 09:02:22
【问题描述】:
我正在对向量(权重)进行简单的归一化,尝试使用 STL 算法使代码尽可能干净(我意识到这对于 for 循环来说非常简单):
float tot = std::accumulate(weights.begin(), weights.end(), 0.0);
std::transform(weights.begin(), weights.end(), [](float x)->float{return(x/tot);});
目前,匿名函数看不到 tot,因此无法编译。使局部变量对匿名函数可见的最佳方法是什么?
【问题讨论】:
-
对不起,0 应该是 0.0!已编辑
标签: c++ lambda c++11 scope anonymous-function