C++ 入家世23天:Lambda 表达式与尺度库算法入门

打印 上一主题 下一主题

主题 898|帖子 898|积分 2694

往期回顾:
   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 表达式的基本语法如下:
  1. [capture](parameters) -> return_type {
  2.     // 函数体
  3. };
复制代码
此中: 


  • [capture]:捕捉列表,定义 Lambda 可以使用的外部变量。
  • (parameters):参数列表,类似平常函数的参数。
  • -> return_type:返回范例,可选。如果可以推导返回范例,可以省略。
  • {}:函数体,包含要实验的代码。
1.3 Lambda 表达式示例

  1. #include <iostream>
  2. using namespace std;
  3. int main() {
  4.     // 简单的 Lambda 表达式
  5.     auto add = [](int a, int b) -> int {
  6.         return a + b;
  7.     };
  8.     cout << "3 + 5 = " << add(3, 5) << endl;
  9.     // 无参数的 Lambda 表达式
  10.     auto greet = []() {
  11.         cout << "Hello, Lambda!" << endl;
  12.     };
  13.     greet();
  14.     return 0;
  15. }
复制代码
  输出效果
  1. 3 + 5 = 8
  2. Hello, Lambda!
复制代码
1.4 捕捉列表的用法

Lambda 表达式可以通过捕捉列表访问外部变量:
  1. #include <iostream>
  2. using namespace std;
  3. int main() {
  4.     int x = 10;
  5.     int y = 20;
  6.     // 捕获 x 和 y
  7.     auto sum = [x, y]() {
  8.         return x + y;
  9.     };
  10.     cout << "x + y = " << sum() << endl;
  11.     // 捕获所有变量的引用
  12.     auto increment = [&]() {
  13.         x++;
  14.         y++;
  15.     };
  16.     increment();
  17.     cout << "After increment, x = " << x << ", y = " << y << endl;
  18.     return 0;
  19. }
复制代码
  输出效果
  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

  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. using namespace std;
  5. int main() {
  6.     vector<int> numbers = {5, 2, 9, 1, 5, 6};
  7.     // 对容器排序
  8.     sort(numbers.begin(), numbers.end());
  9.     cout << "Sorted numbers: ";
  10.     for (int n : numbers) {
  11.         cout << n << " ";
  12.     }
  13.     cout << endl;
  14.     return 0;
  15. }
复制代码

   输出效果
  1. Sorted numbers: 1 2 5 5 6 9
复制代码
2.3.2 使用 std::find

  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. using namespace std;
  5. int main() {
  6.     vector<int> numbers = {5, 2, 9, 1, 5, 6};
  7.     // 查找元素 9
  8.     auto it = find(numbers.begin(), numbers.end(), 9);
  9.     if (it != numbers.end()) {
  10.         cout << "Found 9 at index: " << distance(numbers.begin(), it) << endl;
  11.     } else {
  12.         cout << "9 not found" << endl;
  13.     }
  14.     return 0;
  15. }
复制代码

   输出效果
  1. Found 9 at index: 2
复制代码
2.3.3 使用 std::for_each

  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. using namespace std;
  5. int main() {
  6.     vector<int> numbers = {1, 2, 3, 4, 5};
  7.     // 遍历容器并打印每个元素
  8.     for_each(numbers.begin(), numbers.end(), [](int n) {
  9.         cout << n * n << " ";  // 打印平方
  10.     });
  11.     cout << endl;
  12.     return 0;
  13. }
复制代码

   输出效果
  1 4 9 16 25
  2.3.4 使用 std::count

  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. using namespace std;
  5. int main() {
  6.     vector<int> numbers = {1, 2, 3, 1, 1, 4, 1};
  7.     // 统计元素 1 的出现次数
  8.     int count_of_ones = count(numbers.begin(), numbers.end(), 1);
  9.     cout << "Count of 1: " << count_of_ones << endl;
  10.     return 0;
  11. }
复制代码

   输出效果
  1. Count of 1: 4
复制代码
2.3.5 使用 std::accumulate

  1. #include <iostream>
  2. #include <vector>
  3. #include <numeric>
  4. using namespace std;
  5. int main() {
  6.     vector<int> numbers = {1, 2, 3, 4, 5};
  7.     // 计算累计和
  8.     int total = accumulate(numbers.begin(), numbers.end(), 0);
  9.     cout << "Total sum: " << total << endl;
  10.     return 0;
  11. }
复制代码

   输出效果
  1. Total sum: 15
复制代码

结语

以上就是 C++ 中的 Lambda 表达式尺度库算法的基础知识点了。Lambda 表达式 是一种简化代码的方法,特别恰当在算法函数中使用。尺度库算法 提供了多种容器操作工具,可以大幅减少代码量,提高开发效率。理解它们的特性和使用方法,可以显著提升我们的代码效率。编写更加简洁、优雅的 C++ 程序。
都看到这里了,点个赞再走呗朋侪~
加油吧,预祝大家变得更强!

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

饭宝

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表