往期回顾:
C++ 入家世 20 天:STL 容器之无序集合与无序多重集合-CSDN博客
C++ 入家世 21 天:STL 容器之无序映射与无序多重映射-CSDN博客
C++ 学习第22天:智能指针与异常处理-CSDN博客
C++ 入家世23天:Lambda 表达式与尺度库算法入门
前言
今天我们学习两个重要的 C++ 特性:Lambda 表达式 和 尺度库中的算法。这两个特性是当代 C++ 编程的重要构成部分,能让我们的代码更简洁、高效。
1. Lambda 表达式
1.1 什么是 Lambda 表达式?
Lambda 表达式是 C++11 引入的一种匿名函数,用于简化代码。它答应我们直接在须要的地方定义函数,而不须要单独声明或定义。
1.2 Lambda 表达式的语法
Lambda 表达式的基本语法如下:
- [capture](parameters) -> return_type {
- // 函数体
- };
复制代码 此中:
- [capture]:捕捉列表,定义 Lambda 可以使用的外部变量。
- (parameters):参数列表,类似平常函数的参数。
- -> return_type:返回范例,可选。如果可以推导返回范例,可以省略。
- {}:函数体,包含要实验的代码。
1.3 Lambda 表达式示例
- #include <iostream>
- using namespace std;
- int main() {
- // 简单的 Lambda 表达式
- auto add = [](int a, int b) -> int {
- return a + b;
- };
- cout << "3 + 5 = " << add(3, 5) << endl;
- // 无参数的 Lambda 表达式
- auto greet = []() {
- cout << "Hello, Lambda!" << endl;
- };
- greet();
- return 0;
- }
复制代码 输出效果:
1.4 捕捉列表的用法
Lambda 表达式可以通过捕捉列表访问外部变量:
- #include <iostream>
- using namespace std;
- int main() {
- int x = 10;
- int y = 20;
- // 捕获 x 和 y
- auto sum = [x, y]() {
- return x + y;
- };
- cout << "x + y = " << sum() << endl;
- // 捕获所有变量的引用
- auto increment = [&]() {
- x++;
- y++;
- };
- increment();
- cout << "After increment, x = " << x << ", y = " << y << endl;
- return 0;
- }
复制代码 输出效果:
x + y = 30 After increment, x = 11, y = 21
2. 尺度库算法
2.1 什么是尺度库算法?
C++ 的尺度库算法头文件 <algorithm> 提供了一组常用的算法函数,用于处理容器中的数据,比方排序、查找、遍历等。
2.2 常用算法函数
以下是一些常用的尺度库算法:
- std::sort:排序。
- std::find:查找元素。
- std::for_each:对容器中的每个元素实验操作。
- std::count:统计特定值的出现次数。
- std::accumulate:计算容器中所有元素的累计和(须要 <numeric> 头文件)。
2.3 示例代码
2.3.1 使用 std::sort
- #include <iostream>
- #include <vector>
- #include <algorithm>
- using namespace std;
- int main() {
- vector<int> numbers = {5, 2, 9, 1, 5, 6};
- // 对容器排序
- sort(numbers.begin(), numbers.end());
- cout << "Sorted numbers: ";
- for (int n : numbers) {
- cout << n << " ";
- }
- cout << endl;
- return 0;
- }
复制代码
输出效果:
- Sorted numbers: 1 2 5 5 6 9
复制代码 2.3.2 使用 std::find
- #include <iostream>
- #include <vector>
- #include <algorithm>
- using namespace std;
- int main() {
- vector<int> numbers = {5, 2, 9, 1, 5, 6};
- // 查找元素 9
- auto it = find(numbers.begin(), numbers.end(), 9);
- if (it != numbers.end()) {
- cout << "Found 9 at index: " << distance(numbers.begin(), it) << endl;
- } else {
- cout << "9 not found" << endl;
- }
- return 0;
- }
复制代码
输出效果:
2.3.3 使用 std::for_each
- #include <iostream>
- #include <vector>
- #include <algorithm>
- using namespace std;
- int main() {
- vector<int> numbers = {1, 2, 3, 4, 5};
- // 遍历容器并打印每个元素
- for_each(numbers.begin(), numbers.end(), [](int n) {
- cout << n * n << " "; // 打印平方
- });
- cout << endl;
- return 0;
- }
复制代码
输出效果:
1 4 9 16 25
2.3.4 使用 std::count
- #include <iostream>
- #include <vector>
- #include <algorithm>
- using namespace std;
- int main() {
- vector<int> numbers = {1, 2, 3, 1, 1, 4, 1};
- // 统计元素 1 的出现次数
- int count_of_ones = count(numbers.begin(), numbers.end(), 1);
- cout << "Count of 1: " << count_of_ones << endl;
- return 0;
- }
复制代码
输出效果:
2.3.5 使用 std::accumulate
- #include <iostream>
- #include <vector>
- #include <numeric>
- using namespace std;
- int main() {
- vector<int> numbers = {1, 2, 3, 4, 5};
- // 计算累计和
- int total = accumulate(numbers.begin(), numbers.end(), 0);
- cout << "Total sum: " << total << endl;
- return 0;
- }
复制代码
输出效果:
结语
以上就是 C++ 中的 Lambda 表达式 和 尺度库算法的基础知识点了。Lambda 表达式 是一种简化代码的方法,特别恰当在算法函数中使用。尺度库算法 提供了多种容器操作工具,可以大幅减少代码量,提高开发效率。理解它们的特性和使用方法,可以显著提升我们的代码效率。编写更加简洁、优雅的 C++ 程序。
都看到这里了,点个赞再走呗朋侪~
加油吧,预祝大家变得更强!
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |