Dan*_*Dan 14 c++ lambda visual-c++-2010 c++11
使用C++ 0x,当我在lambda中有lambda时如何捕获变量?例如:
std::vector<int> c1;
int v = 10; <--- I want to capture this variable
std::for_each(
c1.begin(),
c1.end(),
[v](int num) <--- This is fine...
{
std::vector<int> c2;
std::for_each(
c2.begin(),
c2.end(),
[v](int num) <--- error on this line, how do I recapture v?
{
// Do something
});
});
Run Code Online (Sandbox Code Playgroud)
std::for_each(
c1.begin(),
c1.end(),
[&](int num)
{
std::vector<int> c2;
int& v_ = v;
std::for_each(
c2.begin(),
c2.end(),
[&](int num)
{
v_ = num;
}
);
}
);
Run Code Online (Sandbox Code Playgroud)
不是特别干净,但确实有效.
| 归档时间: |
|
| 查看次数: |
3985 次 |
| 最近记录: |