函数包装器和lambda表达式

简介: 函数包装器和lambda表达式

函数包装器

使用:

1.头文件 functional

2. function<函数返回值类型(参数类型)> 对象(函数名)

当然对象也可以通过赋值的操作

作用:提供一个类型的公共接口,方便使用模板

包装一般的函数

#include<iostream>
#include<functional>
using namespace std;
int Max(int a, int b)
{
  return a > b ? a : b;
}
int main()
{
  function<int(int, int)> FuncMax(Max);
  cout << FuncMax(1, 2) << endl;
  system("pause");
  return 0;
}

包装静态成员函数

#include<iostream>
#include<string>
#include<functional>
using namespace std;
class MM
{
public:
  static void fun(int a, int b)
  {
    cout << a + b << endl;
  }
};
int main()
{
  function<void(int, int)> funct(MM::fun); 
  funct(2, 2);
  function<void(int, int)> functi = &MM::fun;//也可以通过赋值的操作
  functi(3, 5);
  system("pause");
  return 0;
}

包装仿函数

仿函数的参数一般用const来修饰

#include <functional>
#include <string>
#include <iostream>
using namespace std;
class Test
{
public:
  void operator()(const string& str)
  {
    cout << str << endl;
  }
};
int main()
{
  //3.包装仿函数
  Test test;
  function<void(const string&)> Func = test;
  Func("仿函数");
  test("仿函数");
  return 0;
}

包装隐式转换的对象

#include<iostream>
#include<string>
#include<functional>
using namespace std;
using func = void(*) (int, int); //取别名
//类型与fun函数类型一样
class MM
{
public:
  static void fun(int a, int b)
  {
    cout << a + b << endl;
  }
  operator func()
  {
    return fun;
  }
};
int main()
{
  //包含隐式转换的对象
  MM mm;
  function<void(int, int)> ff = mm;
  ff(2, 4);
  system("pasue");
  return 0;
}
#lambda表达式
> lambda表达式得到的结果是一个函数指针,并且函数指针指向和赋值都一步到位了。
## lambda表达式的写法
```cpp
写法:
[捕获方式](参数列表)mutable noexcept->函数返回值类型{函数体;}
使用的时候:直接使用auto类型推断类型接受1.lambda表达式的返回值
2.使用的时候除了捕获方式和函数之外,所有的东西都可以省略
捕获的方式:指的是lambda函数体使用之外的变量的方式
[] 不捕获任何东西
[=] 用值的方式
[&] 引用的方式
[this] 成员函数
[&value] value用的引用的方式
[=, &value] value用的引用的方式,其他变量用值的方式

lambda表达式的基本操作

1.学会使用一个完成版的lambda表达式

2.用lambda表达式调用函数

3.lambda缺省的写法

4.lambda捕获方式区别

完整写法

include<iostream>
#include<string>
using namespace std;
int main()
{
  int a = 2, b = 5;
  auto fun = [](int a, int b) mutable noexcept->int{ return a > b ? a : b; };
  cout << fun(a, b) << endl;
  //写到一起
  cout << [](int a, int b) mutable noexcept -> int {return a > b ? a : b; }(a, b) << endl;;
  system("pause");
  return 0;
}

缺省写法(以及=和&捕获方式的区别)

#include<iostream>
#include<string>
using namespace std;
int main()
{
  int a = 4, b = 9;
  cout << [](int a, int b) {return a > b ? a : b; }(a, b) << endl;
  cout << [=] {return a > b ? a : b; }() << endl;
  //=捕获方式不改变值的大小
  b = 19;
  cout << [=] {return a > b ? a : b; }() << endl;
  //&捕获方式改变值的大小
  b = 19;
  cout << [&] {return a > b ? a : b; }() << endl;
  system("pause");
  return 0;
}

this捕获方式

#include<iostream>
#include<string>
using namespace std;
class MM
{
public:
  MM() = default;
  void printDate()
  {
    cout << [this]()-> int {return this->age++; }() << endl;
  }
private:
  int age = 10 ;
  string name;
};
int main()
{
  MM mm;
  mm.printDate();
  system("pause");
  return 0;
}

lambda可以用在以函数指针充当函数参数的地方

一般结合算法或者函数使用

这里举了2个例子,find_if算法和for_each算法

算法后续会介绍

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
  vector<int> date = { 3, 1, 4, 5, 1, 9 };
  for_each(date.begin(), date.end(), [](int date) {cout << date; });
  cout << endl;
  cout << *find_if(date.begin(), date.end(), [](int date) {return date == 5; }) << endl;
  return 0;
}


相关文章
|
缓存 监控 算法
JAVA内存深度分析报告
JAVA内存深度分析报告
745 0
|
安全
Introduce Intelligence to Your Security Operations
http://cyber.lockheedmartin.com/blog/introduce-intelligence-to-your-security-operations ...
852 0
|
3天前
|
存储 弹性计算 人工智能
【2025云栖精华内容】 打造持续领先,全球覆盖的澎湃算力底座——通用计算产品发布与行业实践专场回顾
2025年9月24日,阿里云弹性计算团队多位产品、技术专家及服务器团队技术专家共同在【2025云栖大会】现场带来了《通用计算产品发布与行业实践》的专场论坛,本论坛聚焦弹性计算多款通用算力产品发布。同时,ECS云服务器安全能力、资源售卖模式、计算AI助手等用户体验关键环节也宣布升级,让用云更简单、更智能。海尔三翼鸟云服务负责人刘建锋先生作为特邀嘉宾,莅临现场分享了关于阿里云ECS g9i推动AIoT平台的场景落地实践。
【2025云栖精华内容】 打造持续领先,全球覆盖的澎湃算力底座——通用计算产品发布与行业实践专场回顾
|
1天前
|
云安全 人工智能 自然语言处理
阿里云x硅基流动:AI安全护栏助力构建可信模型生态
阿里云AI安全护栏:大模型的“智能过滤系统”。
|
2天前
|
人工智能 自然语言处理 自动驾驶
关于举办首届全国大学生“启真问智”人工智能模型&智能体大赛决赛的通知
关于举办首届全国大学生“启真问智”人工智能模型&智能体大赛决赛的通知
|
5天前
|
存储 机器学习/深度学习 人工智能
大模型微调技术:LoRA原理与实践
本文深入解析大语言模型微调中的关键技术——低秩自适应(LoRA)。通过分析全参数微调的计算瓶颈,详细阐述LoRA的数学原理、实现机制和优势特点。文章包含完整的PyTorch实现代码、性能对比实验以及实际应用场景,为开发者提供高效微调大模型的实践指南。
555 2
|
3天前
|
Linux 虚拟化 iOS开发
VMware Workstation Pro 25H2 for Windows & Linux - 领先的免费桌面虚拟化软件
VMware Workstation Pro 25H2 for Windows & Linux - 领先的免费桌面虚拟化软件
793 4
VMware Workstation Pro 25H2 for Windows & Linux - 领先的免费桌面虚拟化软件
kde
|
5天前
|
人工智能 关系型数据库 PostgreSQL
n8n Docker 部署手册
n8n是一款开源工作流自动化平台,支持低代码与可编程模式,集成400+服务节点,原生支持AI与API连接,可自托管部署,助力团队构建安全高效的自动化流程。
kde
374 3