????3. lambda
??????????function??bind?? ????????????lambda????python??????????????????lambda???????????????????????????????????潲??closure?????lambda?????????closure??????????????????????????????????潲??function??bind??????????????????????????????????????lambda???????????
vector< int> vec;
/// 1. simple lambda
auto it = std::find_if(vec.begin()?? vec.end()?? [](int i) { return i > 50; });
class A
{
public:
bool operator(int i) const { return i > 50; }
};
auto it = std::find_if(vec.begin()?? vec.end()?? A());
/// 2. lambda return syntax
std::function< int(int)> square = [](int i) -> int { return i * i; }
/// 3. lambda expr: capture of local variable
{
int min_val = 10;
int max_val = 1000;
auto it = std::find_if(vec.begin()?? vec.end()?? [=](int i) {
return i > min_val && i < max_val;
});
auto it = std::find_if(vec.begin()?? vec.end()?? [&](int i) {
return i > min_val && i < max_val;
});
auto it = std::find_if(vec.begin()?? vec.end()?? [=?? &max_value](int i) {
return i > min_val && i < max_val;
});
}
/// 4. lambda expr: capture of class member
class A
{
public:
void DoSomething();
private:
std::vector<int>  m_vec;
int               m_min_val;
int               m_max_va;
};
/// 4.1 capture member by this
void A::DoSomething()
{
auto it = std::find_if(m_vec.begin()?? m_vec.end()?? [this](int i){
return i > m_min_val && i < m_max_val; });
}
/// 4.2 capture member by default pass-by-value
void A::DoSomething()
{
auto it = std::find_if(m_vec.begin()?? m_vec.end()?? [=](int i){
return i > m_min_val && i < m_max_val; });
}
/// 4.3 capture member by default pass-by-reference
void A::DoSomething()
{
auto it = std::find_if(m_vec.begin()?? m_vec.end()?? [&](int i){
return i > m_min_val && i < m_max_val; });
}
???????????????????????lambda????????÷???????????????????????????????????????????1??2??3??4??£???
??????1?????????lambda?????????????????lambda??????find_if???????????functor??find_if?????
??????2????????з??????lambda?????????????????????????????->д??????б????????檔???????????????????????????
????a. ???????void?????
????b. lambda??????body????return expr????expr????????????????
??????3???????lambda????capture?????????????????????????С??????????capture???????????????С??????=???capture?????pass-by-value?? ?????С?ó???&???capture?????pass-by-reference????????С????????????default??pass-by-value?? ????max_value???????pass-by-reference
??????4???????lambda????capture??????????????????????????С??????????С?????????this?????capture??????????????????????????????????????????????pass-by-value?????????????????pass-by-reference??
?????????????????????????????????1???lambda???????????Щ???????
??????1??lambda????????????????????????????????
????a. ????????????е????????????capture??????????????????????3?????
????b. ??????????????????????
??????2?????????????closure??lambda???????????????壩????????????????????????????closure???????????????????????????????bind?????????????????????????μ?
??????3??????lambda?????????????????closure????closure????????????壬??????????std::function???????????????closure????????????auto
????????????????????????????function?? bind??lambda???÷?????????????????C++?????÷??????е????????ζ?????????????????????????bind??????function????lambda??????????function?? ???????????????ok??????????????????bind????????std::placeholders????????????lambda?????????????????????????????lambda??????