> 文章列表 > 函数包装器和lambda表达式

函数包装器和lambda表达式

函数包装器和lambda表达式

函数包装器和lambda表达式

  • 函数包装器
    • 包装一般的函数
    • 包装静态成员函数
    • 包装仿函数
    • 包装隐式转换的对象
    • lambda表达式的基本操作
      • 完整写法
      • 缺省写法(以及=和&捕获方式的区别)
    • this捕获方式
    • 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;
}