C++ Lambda??????????
???????????? ???????[ 2014/9/16 13:20:09 ] ??????????????? C++ NET
????C++ 11 ??LB?????????????Functional Programming????????????????????????????C++???????C#??JavaScript????????е???????????????????????á?
????????????
?????????????????????????????LB?????????????class????????????????????-????????
?????????????vector???????????????
????vector<int> v1(10?? 1);
????int sum = 0;
????for_each (v1.begin()?? v1.end()?? [&](int i){ sum += i; })??//Line1
????????????????????function?????????????鸴?????????LB??????????function ?????????????????VC++?У?????LB??????????????????????
class _lambda_a01 {
int &capture1_;
public:
_lambda_a01(int &x): capture1_(x) {} //Line2
operator void (int i) { capture1_ += I; }
};
?????????Line1??????????
_lambda_a01 lbd1??sum????
for??auto a??v1??{
ldb1??a????
}
?????????????棬???C++??????LB?????inline expression (inline ????????????????????????????????????LB??????????“type”????????????????LB????????????????λ?????磺
????vector<int> v1(10?? 1);
????int sum = 0;
????for_each (v1.begin()?? v1.end()?? [&](int i){ sum += i; })??//Line1
????vector<int> v2(10?? 1);
????int sum2 = 0;
????for_each (v1.begin()?? v1.end()?? [&](int i){ sum2 += i; })??//Line2
????????????????????????Line1??Line2??????????????????ɡ??????????????LB???????
????????????????
????vector<int> v1(10?? 1);
????vector<int> v2(10?? 1);
????int sum = 0;
????auto lb = [&](int i){ sum += i; }?? //Line0
????for_each (v1.begin()?? v1.end()?? lb)??//Line1
????sum = 0; // Line1.1
????for_each (v1.begin()?? v1.end()?? lb})??//Line2
??????Line0????????????????????lb???????LB????????Line1??Line2?????á?
???????????
????1?? ???LB??“????”????????μ?“????”???????????“????????”???????????size????С????????
????2?? ???????LB?????“????”?????????????????????????????Line1.1???????????????????
????3?? ?????????“?????by value??”???????“???????by reference????????Line0?????by reference.
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11