Lambda表达式与STL算法结合可提升代码简洁性与效率。1. 捕获机制分按值捕获(复制变量,独立于外部变化)和按引用捕获(直接访问变量,同步外部变化),如示例中threshold按值捕获后不随外部修改而变,而按引用捕获则实时响应。2. 自定义排序可通过Lambda作为比较函数传递给std::sort,如按Person对象的age属性排序。3. 实用技巧包括:std::transform用于元素转换,std::remove_if删除满足条件的元素,std::for_each执行遍历操作,std::find_if查找首个匹配元素,均通过Lambda实现灵活逻辑内联,避免冗余函数定义,提升可读性与维护性。

C++ Lambda表达式和STL算法的结合,可以让你写出简洁、高效、可读性强的代码。它们一起工作,就像乐高积木一样,可以构建复杂的功能,而无需编写大量重复的代码。
Lambda表达式,本质上就是一个匿名函数。它允许你在需要函数的地方,直接定义一个函数,而不需要给它命名。STL算法,则是一组预定义的函数,用于执行各种常见的操作,比如排序、查找、转换等。
Lambda表达式 STL算法结合使用
Lambda表达式的捕获机制是理解其工作原理的关键。简单来说,就是lambda表达式如何访问其定义范围之外的变量。它可以按值捕获,也可以按引用捕获。
立即学习“C++免费学习笔记(深入)”;
按值捕获,就像是复制一份变量的副本到lambda表达式内部。这意味着,即使原始变量在lambda表达式外部发生了改变,lambda表达式内部的副本仍然保持不变。
按引用捕获,则像是lambda表达式内部直接指向原始变量。任何对lambda表达式内部变量的修改,都会影响到原始变量。
选择哪种捕获方式,取决于你的需求。如果lambda表达式需要修改原始变量,或者原始变量本身就是一个大型对象,按引用捕获可能更合适。否则,按值捕获可以避免一些潜在的副作用。
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> numbers = {1, 2, 3, 4, 5};
int threshold = 3;
// 按值捕获 threshold
int count_greater_than_threshold_by_value = std::count_if(numbers.begin(), numbers.end(), [threshold](int n){
return n > threshold;
});
std::cout << "Numbers greater than " << threshold << " (by value): " << count_greater_than_threshold_by_value << std::endl;
// 按引用捕获 threshold
int count_greater_than_threshold_by_reference = std::count_if(numbers.begin(), numbers.end(), [&threshold](int n){
return n > threshold;
});
std::cout << "Numbers greater than " << threshold << " (by reference): " << count_greater_than_threshold_by_reference << std::endl;
threshold = 2; // 修改 threshold 的值
// 再次计算,观察结果
int count_greater_than_threshold_by_value_after_change = std::count_if(numbers.begin(), numbers.end(), [threshold](int n){
return n > threshold;
});
std::cout << "Numbers greater than " << threshold << " (by value, after change): " << count_greater_than_threshold_by_value_after_change << std::endl; // 结果不变
int count_greater_than_threshold_by_reference_after_change = std::count_if(numbers.begin(), numbers.end(), [&threshold](int n){
return n > threshold;
});
std::cout << "Numbers greater than " << threshold << " (by reference, after change): " << count_greater_than_threshold_by_reference_after_change << std::endl; // 结果改变
return 0;
}STL的
std::sort
<
Lambda表达式可以作为一个比较函数,传递给
std::sort
例如,假设我们有一个
Person
name
age
Person
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
struct Person {
std::string name;
int age;
};
int main() {
std::vector<Person> people = {
{"Alice", 30},
{"Bob", 25},
{"Charlie", 35}
};
// 使用 Lambda 表达式按照年龄排序
std::sort(people.begin(), people.end(), [](const Person& a, const Person& b) {
return a.age < b.age;
});
for (const auto& person : people) {
std::cout << person.name << ": " << person.age << std::endl;
}
return 0;
}除了排序,Lambda表达式还可以和很多其他的STL算法结合使用,实现各种各样的功能。
std::transform
std::remove_if
std::for_each
std::find_if
这些只是冰山一角。Lambda表达式的灵活性和STL算法的强大功能,可以让你以简洁高效的方式解决各种编程问题。掌握它们,可以大大提高你的C++编程效率。
以上就是C++ lambda表达式 STL算法结合使用的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号