值捕获、引用捕获
#include <iostream>
using namespace std;
int main(){
size_t v1=42;
auto f=[v1]{return v1;};
auto f2=[&v1]{return v1;};
v1=0;
auto j=f();
auto j1=f2();
cout<<j<<endl;
cout<<j1<<endl;
}
输出
42
0
隐式捕获
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
void biggies(vector<string> &words,vector<string>::size_type sz,ostream &os=cout){
auto wc=find_if(words.begin(),words.end(),[=](const string &s){return s.size()>=sz;});
cout<<*wc<<endl;
}
int main(){
vector<string> v={"abc","bdf","caaaaa"};
biggies(v,4);
}
隐式捕获sz
输出caaaaa