C++(6)
类模版 std::function
类模版 std::function 是可调用对象的包装器,可以包装除了类成员函数之外的所
有可调用对象。包括,普通函数,函数指针, lambda,仿函数。
通过指定的模板参数,它可以用统一的方式保存,并延迟执行它们。所谓的延迟执
行,就是回调了。
std:function<retType(argType,argType,…)>
#include <iostream>
#include <functional>
using namespace std;
// 定义一个加法函数
int add(int a, int b) {
return a + b;
}
// 定义一个减法函数
int sub(int a, int b) {
return a - b;
}
// 定义一个函数指针类型,指向返回值为int,参数为两个int的函数
typedef int (*fun_ptr)(int, int);
// 定义一个类Divide,重载了()运算符,用于实现除法
class Divide{
public:
int operator()(int a, int b){
return a / b;
}
};
int main() {
// 使用std::function包装add函数
auto f = function<int(int, int)>(add);
cout << f(1, 2) << endl; // 输出3
// 使用函数指针p指向sub函数
fun_ptr p = sub;
// 将函数指针p包装进std::function
f = function<int(int, int)>(p);
cout << f(1, 2) << endl; // 输出-1
// 定义一个lambda表达式mul,用于实现乘法
auto mul = [](int a, int b) {
return a * b;
};
// 将lambda表达式mul包装进std::function
f = function<int(int, int)>(mul);
cout << f(2, 3) << endl; // 输出6
// 创建Divide类的实例div
Divide div;
// 将div对象包装进std::function
f = function<int(int, int)>(div);
cout << f(10, 3) << endl; // 输出3
return 0;
}
map中使用
#include <iostream>
#include <map>
#include <functional>
using namespace std;
// 定义一个加法函数
int add(int a, int b) {
return a + b;
}
// 定义一个减法函数
int sub(int a, int b) {
return a - b;
}
// 定义一个函数指针类型,指向返回值为int,参数为两个int的函数
typedef int (*fun_ptr)(int, int);
// 定义一个类Divide,重载了()运算符,用于实现除法
class Divide{
public:
int operator()(int a, int b){
return a / b;
}
};
int main(){
// 定义一个lambda表达式mul,用于实现乘法
auto mul = [](int a, int b) {
return a * b;
};
map< string , function<int(int,int)> > mp={
{"+",add},
{"-",sub},
{"*",mul},
{"/",Divide()}
};
// 调用map中保存的函数
cout<<mp["+"](1,2)<<endl;//3
cout<<mp["-"](1,2)<<endl;//-1
cout<<mp["*"](2,3)<<endl;//6
cout<<mp["/"](10,3)<<endl;//3
return 0;
}
fucntion 作参数类型实现回调
#include <iostream>
#include <functional>
#include <algorithm>
using namespace std;
// 选择排序函数,接受一个整数数组指针、数组长度和一个比较函数
void selsctSort(int *p, int n, function<int(int, int)> com) {
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
// 使用传入的比较函数com来比较当前元素p[i]和p[j]
if (com(p[i], p[j])) {
p[i] ^= p[j];
p[j] ^= p[i];
p[i] ^= p[j];
}
}
}
}
int main() {
int arr[] = {5, 3, 8, 6, 2, 7, 4, 1};
int n = sizeof(arr) / sizeof(*arr);
// 调用选择排序函数,传入数组、数组长度和一个lambda表达式作为比较函数
// 该lambda表达式定义了比较规则
selsctSort(arr, n, [](int a, int b) { return a > b; });
// 使用for_each算法遍历排序后的数组,并输出每个元素
// 传入的lambda表达式用于定义对每个元素的操作,这里只是简单地输出元素
for_each(arr, arr + n, [](int &a) { cout << a << " "; });
return 0;
}
fucntion 作类成员实现回调
#include <iostream>
#include <list>
#include <functional>
using namespace std;
// 定义三个仿函数类,每个类重载了()操作符,用于执行特定的操作
class FunctorA
{
public:
void operator()(){
cout << "class Functor A" << endl;
}
};
class FunctorB
{
public:
void operator()(){
cout << "class Functor B" << endl;
}
};
class FunctorC
{
public:
void operator()(){
cout << "class Functor C" << endl;
}
};
class Object
{
public:
Object(FunctorA a, FunctorB b, FunctorC c)
{
_list.push_back(a);
_list.push_back(b);
_list.push_back(c);
}
void notify()
{
for(auto &func : _list){
func();
}
}
private:
// 使用std::function模板来存储可调用对象
list< function<void(void)> > _list;
};
int main()
{
FunctorA a;
FunctorB b;
FunctorC c;
Object obj(a, b, c);
obj.notify();
return 0;
}