前言
在现代C++中,Lambda表达式提供了一种简洁而强大的方式来定义匿名函数,使代码更具可读性和灵活性。自C++11引入Lambda以来,它已经成为STL算法、并发编程和回调机制中的重要工具。随着C++14、C++17和C++20的不断演进,Lambda的功能也在不断增强,进一步提升了C++语言的表达能力。
本文将从Lambda的语法入手,分析其捕捉列表的工作原理,探讨Lambda在实际开发中的应用,并深入剖析Lambda背后的实现原理,帮助大家全面掌握这一强大特性。
1.表达式语法介绍
简单的lambda表达式实例
#include <iostream>
using namespace std;
int main() {auto add=[](int x,int y)->int {return x+y;};cout<<add(1,2)<<endl;return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
int main() {auto add = [](int x, int y)->int {return x + y; };cout << add(1, 2) << endl;auto print = [] {cout << "hello world" << endl; };print();int a = 0, b = 1;auto swap = [](int& x, int& y) {int temp = x;x = y;y = temp;};swap(a, b);cout << a << " " << b << endl;return 0;
}
2.捕捉列表分析
int x = 0;
auto func1 = [] {x++;};
// 捕捉列表必须为空,因为全局变量不⽤捕捉就可以⽤,没有可被捕捉的变量
int main() {int a = 0, b = 1, c = 2, d = 3;auto func2 = [a, &b] {//a++;不可修改b++;x++;int ret = a + b + x;return ret;};cout << func2() << endl;
}
只能用当前局部域的对象和捕捉以及与全局变量,值捕捉不能修改,引用捕捉可以修改
// 隐式捕捉 隐式引⽤捕捉
// ⽤了哪些变量就捕捉哪些变量
auto func2 = [=] {int ret = a + b + c + d; return ret; };
cout << func2() << endl;
auto func3 = [&] {a++; b++; c++; };
func3();
cout<<"a="<<a<<" b="<<b<<" c="<<c<<" d="<<d << endl;
auto func4 = [&, a, b]{//a++;//b++;c++;d++;return a + b + c + d;};
cout<<func4()<<endl;
cout << a << " " << b << " " << c << " " << d << endl;
// 混合捕捉1
auto func5 = [=, &a, &b]{a++;b++;/*c++;d++;*/return a + b + c + d;};
cout<<func5()<<endl;
cout << a << " " << b << " " << c << " " << d << endl;
static int m = 5;
auto func6 = [] {return x + m; };
cout << func6() << endl;
auto func7 = [=]()mutable{a++;b++;c++;d++;return a + b + c + d;};
3.lambda的应用
struct books {string name;int price;books(const string& name, int price): name(name), price(price){}
};
struct compareLess {bool operator()(const books& a, const books& b) {return a.price < b.price;}};
struct compareGreater {bool operator()(const books& a, const books& b) {return a.price > b.price;}
};
int main() {vector<books> books = { {"西游记",45},{"红楼梦",54},{"三国演义",42}};sort(books.begin(), books.end(), compareGreater());sort(books.begin(), books.end(), compareLess());return 0;
}
对于自定义对象的排序,我们采用了仿函数的形式,定义了相关的类,如果有很多种比较情况,就要写很多,这时我们可以采用仿函数的形式。
sort(books.begin(), books.end(), [](const Books& a, const Books& b) {return a.price < b.price;});
sort(books.begin(), books.end(), [](const Books& a, const Books& b) {return a.price > b.price;});
sort(books.begin(), books.end(), [](const Books& a, const Books& b) {return a.id < b.id;});
sort(books.begin(), books.end(), [](const Books& a, const Books& b) {return a.id > b.id;});
4.lambda的原理
class Rate
{
public:Rate(double rate): _rate(rate){}double operator()(double money, int year){return money * _rate * year;}
private:double _rate;
};
int main()
{double rate = 0.49;// lambdaauto r2 = [rate](double money, int year) {return money * rate * year;};// 函数对象Rate r1(rate);r1(10000, 2);r2(10000, 2);auto func1 = [] {cout << "hello world" << endl;};func1();return 0;
}
结束语
Lambda表达式的引入极大地提升了C++的编程体验,使得函数式编程风格更易于在C++中实现。无论是简化代码、提升可读性,还是提高运行时效率,Lambda都扮演着重要的角色。理解其语法、捕捉列表以及底层原理,不仅能帮助开发者更好地使用Lambda,还能在需要时优化其性能。
希望本文能帮助你深入理解C++ Lambda,并在实际开发中更高效地运用这一特性。如果你有任何问题或想法,欢迎在评论区交流探讨!