0.使用場景---只有一兩個地方使用的簡單操作
獨立出來一個函數,但這個函數實現相對簡單並且可能在整個項目只使用了一次(即不存在復用的情況),那么這個時候我們就可以考慮使用下lambda表達式了。
?既然只使用一次,那直接寫全代碼不久醒了,為啥要函數呢?——因為lambda可以捕獲局部變量
bool check_size(const string &s, string::size_type sz) { return s.size() >= sz; }
//wc:第一個滿足size>sz的元素 auto wc = find_if (words.begin(),words.end(),[sz](const string &a) {return a.size()>=sz;});
函數check_size()無法作為find_if的參數。而且需要考慮如何
參考:https://blog.csdn.net/qq_34199383/article/details/80469780
0.1 比較大小
//1.傳統方法 #include <iostream> #include <vector> #include <algorithm> using namespace std; bool compare(int& a, int& b) { return a > b; } int main(void) { int data[6] = { 3, 4, 12, 2, 1, 6 }; vector<int> testdata; testdata.insert(testdata.begin(), data, data + 6); // 排序算法 sort(testdata.begin(), testdata.end(), compare); // 升序 return 0; } /*******************************************/ //2.lambda表達式 #include <iostream> #include <vector> #include <algorithm> using namespace std; int main(void) { int data[6] = { 3, 4, 12, 2, 1, 6 }; vector<int> testdata; testdata.insert(testdata.begin(), data, data + 6); sort(testdata.begin(), testdata.end(), [](int a, int b){ return a > b; }); return 0; }
0.2 auto 和function 接受lambda表達式的返回
#include <iostream> #include <functional> using namespace std; int main(void) { int x = 8, y = 9; auto add = [](int a, int b) { return a + b; }; std::function<int(int, int)> Add = [=](int a, int b) { return a + b; }; cout << "add: " << add(x, y) << endl;//17 cout << "Add: " << Add(x, y) << endl;//17 return 0; }
0.3 使用lambda表達式來實現遞歸算法
#include <iostream> #include <functional> using namespace std; int main(void) { std::function<int(int)> recursion = [&recursion](int n) { return n < 2 ? 1 : recursion(n - 1) + recursion(n - 2); }; // 我們來檢測下我們的結果 cout << "recursion(2):" << recursion(2) << endl;//2 cout << "recursion(3):" << recursion(3) << endl;//3 cout << "recursion(4):" << recursion(4) << endl;//5 return 0; }
1.本質:未命名的內聯函數
[capture list] (parameter list) ->return type { function body } //capture list 捕獲列表,函數中定義的局部變量列表,通常為空 //lambda表達式必須使用尾置返回——return **
2.可以忽略返回類型,參數列表,但必須包含捕獲列表和函數體
auto f =[] {return 42;}
3.捕獲列表
auto wc = find_if(words.begin(), words.end(),[sz](const string &a) { return a.size() >= sz;} );
4.變量截取的方式
- [] 不截取任何變量
- [&] 截取外部作用域中所有變量,並作為引用在函數體中使用
- [=] 截取外部作用域中所有變量,並拷貝一份在函數體中使用
- [=, &foo] 截取外部作用域中所有變量,並拷貝一份在函數體中使用,但是對foo變量使用引用
- [bar] 截取bar變量並且拷貝一份在函數體重使用,同時不截取其他變量
- [this] 截取當前類中的this指針。如果已經使用了&或者=就默認添加此選項。
5.lambda表達式產生的類不含有默認構造函數、賦值運算符、默認析構函數;
6.lambda表達式是一種簡易的定義函數對象類的方式。??如何定義?有何用處?
l參考:
https://www.cnblogs.com/smiler/p/4095723.html
5.返回類型:單一的return語句;多語句則默認返回void;否則報錯,應指定返回類型
//正確,單一return語句 transform(vi.begin(),vi.end(),vi.begin(), [] (int i) { return i<0? -i; i;}); //錯誤。不能推斷返回類型 transform(vi.begin(),vi.end(),vi.begin(), [] (int i) { if (I<0) return -i; else return i;}) //正確,尾置返回類型 transform(vi.begin(),vi.end(),vi.begin(), [] (int i) ->int { if (I<0) return -i; else return i;})
6.bind()
7.using std::placeholders::_1;
8.示例
stable_sort(words.begin(), words.end(), [](const string& a, const string& b) { return a.size()<b.size();});
class ShorterString{ public: bool opereator()(const string &s1,const string &s2) const {return s1.size()<s2.size();} };
stable_sort(words.begin(), words.end(), ShorterString{));
auto wc = find_if(words.begin(), words.end(), [sz] (const string& a) { return a.size()>=sz;});//返回第一個指向滿足條件元素的迭代器
class SizeComp{ SizeComp(size_t n):sz(n) { } bool operator()(const string &s) const { return s.size()>=sz;} private: size_t sz; }; auto wc = find_if(words.begin(), words.end(),SizeComp(sz));