一、Lambda 表达式简介
Lambda 表达式是 C++11 标准引入的特性,用于创建匿名函数对象(闭包)。它允许你在代码中直接定义一个函数,而无需显式命名。基本语法结构如下:
[capture](parameters) mutable -> return_type {
// 函数体
}
各部分含义:
二、捕获列表详解
捕获方式决定了 Lambda 如何访问外部作用域的变量:
int a = 10;
auto lambda = [a] { return a * 2; }; // 捕获a的值
int b = 20;
auto lambda = [&b] { b += 5; }; // 通过引用修改b
- [=]:捕获所有外部变量(值方式)
- [&]:捕获所有外部变量(引用方式)
- [=, &x]:值捕获所有变量,但x用引用捕获
三、典型应用场景
std::vector<int> nums{3,1,4,2};
std::sort(nums.begin(), nums.end(),
[](int a, int b) { return a > b; }); // 降序排序
std::thread t([=] {
std::cout << "Value captured: " << a << std::endl;
});
t.join();
auto defer = [](auto func) {
return std::make_shared<std::function<void()>>(func);
};
四、C++14/17 增强特性
auto generic = [](auto x, auto y) { return x + y; };
constexpr auto square = [](int n) { return n * n; };
static_assert(square(5) == 25);
五、注意事项
int counter = 0;
auto inc = [counter]() mutable { return ++counter; }; // 修改副本
六、示例:自定义比较器
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<std::string> words{"apple", "banana", "cherry"};
// 按长度排序
std::sort(words.begin(), words.end(),
[](const std::string& a, const std::string& b) {
return a.length() < b.length();
});
for (const auto& w : words) {
std::cout << w << " ";
}
// 输出:apple cherry banana
}
Lambda 表达式极大提升了代码的灵活性和简洁性,是现代 C++ 开发的核心工具之一。
好的,我们来详细讲解 C++ 中的 Lambda 表达式。
一、Lambda 表达式简介
Lambda 表达式是 C++11 标准引入的特性,用于创建匿名函数对象(闭包)。它允许你在代码中直接定义一个函数,而无需显式命名。基本语法结构如下:
[capture](parameters) mutable -> return_type {
// 函数体
}
各部分含义:
二、捕获列表详解
捕获方式决定了 Lambda 如何访问外部作用域的变量:
int a = 10;
auto lambda = [a] { return a * 2; }; // 捕获a的值
int b = 20;
auto lambda = [&b] { b += 5; }; // 通过引用修改b
- [=]:捕获所有外部变量(值方式)
- [&]:捕获所有外部变量(引用方式)
- [=, &x]:值捕获所有变量,但x用引用捕获
三、典型应用场景
std::vector<int> nums{3,1,4,2};
std::sort(nums.begin(), nums.end(),
[](int a, int b) { return a > b; }); // 降序排序
std::thread t([=] {
std::cout << "Value captured: " << a << std::endl;
});
t.join();
auto defer = [](auto func) {
return std::make_shared<std::function<void()>>(func);
};
四、C++14/17 增强特性
auto generic = [](auto x, auto y) { return x + y; };
constexpr auto square = [](int n) { return n * n; };
static_assert(square(5) == 25);
五、注意事项
int counter = 0;
auto inc = [counter]() mutable { return ++counter; }; // 修改副本
六、示例:自定义比较器
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<std::string> words{"apple", "banana", "cherry"};
// 按长度排序
std::sort(words.begin(), words.end(),
[](const std::string& a, const std::string& b) {
return a.length() < b.length();
});
for (const auto& w : words) {
std::cout << w << " ";
}
// 输出:apple cherry banana
}
Lambda 表达式极大提升了代码的灵活性和简洁性,是现代 C++ 开发的核心工具之一。




