函数包装器和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;
}


相关文章
Lambda 语法糖《方法引用》
Lambda 语法糖《方法引用》
|
4月前
|
Java 编译器 Linux
【C++11(二)】lambda表达式以及function包装器
【C++11(二)】lambda表达式以及function包装器
|
4月前
|
存储 算法 C++
C++11:lambda表达式 & 包装器
C++11:lambda表达式 & 包装器
27 0
|
4月前
|
存储 JavaScript 前端开发
对象字面量和对象的封装(结合柯里化)
对象字面量和对象的封装(结合柯里化)
41 0
|
编译器 C++
【C++11】lambda表达式 包装器
【C++11】lambda表达式 包装器
49 0
|
存储 Java 开发者
3.3 函数式接口与Lambda表达式的实际应用:与回调函数的结合
3.3 函数式接口与Lambda表达式的实际应用:与回调函数的结合
87 0
|
Java 开发者
1.4 Lambda表达式的基础:Lambda表达式与匿名类的对比
1.4 Lambda表达式的基础:Lambda表达式与匿名类的对比
83 0
|
存储 Java 开发者
1.1 Lambda表达式的基础:Lambda表达式的定义与语法
1.1 Lambda表达式的基础:Lambda表达式的定义与语法
90 0
|
API
这代码,你不包装下?
不管做什么事情,我们都要有一颗上进的心,写代码也是如此。最开始要写得出,然后要写得对,然后要写得又对又好,最后再追求那个传说中的快。
55 0
|
安全 Java
Lambda、方法引用、函数式接口
Lambda、方法引用、函数式接口