函数包装器和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;
}
  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

温柔了岁月.c

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值