c++函数指针 lambda 表达式 std::function std::bind

 

c/c++ 函数指针的用法

【目录】

基本定义

c 函数指针使用举例

c++ 函数指针使用举例

函数指针作为函数参数

函数指针作为函数返回值

函数指针数组

typedef 简化函数指针操作


 

c语言函数指针的定义形式:返回类型 (*函数指针名称)(参数类型,参数类型,参数类型,…);

c++函数指针的定义形式返回类型 (类名称::*函数成员名称)(参数类型,参数类型,参数类型,….);    

以下代码编译环境:codeblocks with gcc in win 7

c语言函数指针使用举例: 

复制代码
#include <stdio.h>
#include <stdlib.h>

int fun1()
{
    printf("this is fun1 call\n");
    return 1;
}

void fun2(int k, char c)
{
    printf("this is fun2 call:%d %c\n", k, c);
}

int main()
{
    int (*pfun1)() = NULL;
    void (*pfun2)(int, char) = NULL;
    int a,b;
    pfun1 = fun1; //第一种赋值方法
    a = pfun1();  //第一种调用方法(推荐)
    printf("%d\n",a);
    b = (*pfun1)();//第二种调用方法
    printf("%d\n",b);
    pfun2 = &fun2;//第二种赋值方法(推荐,因为和其他数据指针赋值方法一致)
    pfun2(1,'a');
    (*pfun2)(2,'b');
    return 0;
}
复制代码

 

c++函数指针使用举例:

复制代码
#include <iostream>
using namespace std;

class test
{
public:
    test()
    {
        cout<<"constructor"<<endl;
    }
    int fun1(int a, char c)
    {
        cout<<"this is fun1 call:"<<a<<" "<<c<<endl;
        return a;
    }
    void fun2(double d)const
    {
        cout<<"this is fun2 call:"<<d<<endl;
    }
    static double fun3(char buf[])
    {
        cout<<"this is fun3 call:"<<buf<<endl;
        return 3.14;
    }
};

int main()
{
    // 类的静态成员函数指针和c的指针的用法相同
    double (*pstatic)(char buf[]) = NULL;//不需要加类名
    pstatic = test::fun3; //可以不加取地址符号
    pstatic("myclaa");
    pstatic = &test::fun3;
    (*pstatic)("xyz");

    //普通成员函数
    int (test::*pfun)(int, char) = NULL; //一定要加类名
    pfun = &test::fun1; //一定要加取地址符号
    test mytest;
    (mytest.*pfun)(1, 'a'); //调用是一定要加类的对象名和*符号

    //const 函数(基本普通成员函数相同)
    void (test::*pconst)(double)const = NULL; //一定要加const
    pconst = &test::fun2;
    test mytest2;
    (mytest2.*pconst)(3.33);

//    //构造函数或者析构函数的指针,貌似不可以,不知道c++标准有没有规定不能有指向这两者的函数指针
//    (test::*pcon)() = NULL;
//    pcon = &test.test;
//    test mytest3;
//    (mytest3.*pcon)();

    return 0;
}
复制代码

 函数指针作为函数参数:

复制代码
#include <stdio.h>
#include <stdlib.h>

void fun(int k, char c)
{
    printf("this is fun2 call:%d %c\n", k, c);
}

void fun1(void (*pfun)(int, char), int a, char c)
{
    pfun(a, c);
}

int main()
{
    fun1(fun, 1, 'a');
    return 0;
}
// c++ 的形式差不多
复制代码

函数指针作为函数返回值:

复制代码
// c 形式
#include <stdio.h>
#include <stdlib.h>

void fun(int k, char c)
{
    printf("this is fun2 call:%d %c\n", k, c);
}

//fun1 函数的参数为double,返回值为函数指针void(*)(int, char)
void (*fun1(double d))(int, char)
{
    printf("%f\n",d);
    return fun;
}

int main()
{
    void (*p)(int, char) = fun1(3.33);
    p(1, 'a');
    return 0;
}
复制代码
复制代码
//c++ 形式
#include <iostream>
using namespace std;

class test
{
public:
    int fun(int a, char c)
    {
        cout<<"this is fun call:"<<a<<" "<<c<<endl;
        return a;
    }
};

class test2
{
    public:
    // test2 的成员函数fun1,参数是double,
    //返回值是test的成员函数指针int(test::*)(int, char)
    int (test::*fun1(double d))(int, char)
    {
        cout<<d<<endl;
        return &test::fun;
    }
};

int main()
{
    test mytest;
    test2 mytest2;
    int (test::*p)(int, char) = mytest2.fun1(3.33);
    (mytest.*p)(1, 'a');
    return 0;
}
复制代码

函数指针数组:

复制代码
#include <stdio.h>
#include <stdlib.h>

float add(float a,float b){return a+b;}
float minu(float a,float b){return a-b;}

int main()
{
    //定义一个函数指针数组,大小为2
    //里面存放float (*)(float, float)类型的指针
    float (*pfunArry[2])(float, float) = {&add, &minu};
    double k = pfunArry[0](3.33,2.22);// 调用
    printf("%f\n", k);
    k = pfunArry[1](3.33,2.22);
    printf("%f\n", k);
    return 0;
}
//c++ 可类比
复制代码

typedef 简化函数指针类型:

复制代码
#include <stdio.h>
#include <stdlib.h>

float add(float a,float b)
{
    printf("%f\n",a+b);
    return a+b;
}
float minu(float a,float b)
{
    printf("%f\n",a-b);
    return a-b;
}

//用pfunType 来表示float(*)(float, float)
typedef float(*pfunType)(float, float);

int main()
{
    pfunType p = &add;//定义函数指针变量
    p(3.33, 2.22);
    pfunType parry[2] = {&add, &minu};//定义函数指针数组
    parry[1](3.33, 2.22);
    //函数指针作为参数可以定义为:void fun(pfunType p)
    //函数指针作为返回值可以定义为:pfunType fun();

    return 0;
}
//c++ 可类比
复制代码

 【版权声明】转载请注明出处  http://www.cnblogs.com/TenosDoIt/p/3164081.html

来自:https://www.cnblogs.com/TenosDoIt/p/3164081.html

=========

 c++函数指针作为参数:

在C++中,可以通过将函数指针作为参数来实现更高阶的函数,即那些接收另一个函数作为参数的函数。这种技术在需要回调函数或策略模式等场景中非常有用。下面是一个简明的示例,展示了如何定义和使用函数指针作为参数。

示例1:简单的函数指针

假设我们有两个函数,它们都符合某种签名(即参数和返回类型一致),然后我们定义一个接受这些函数指针作为参数的函数:

cpp
#include <iostream> // 定义一个函数类型:返回类型为int,参数为两个int typedef int (*Operation)(int, int); // 定义两个符合这个签名的函数 int add(int a, int b) { return a + b; } int multiply(int a, int b) { return a * b; } // 接受函数指针作为参数的函数 void executeOperation(int x, int y, Operation op) { std::cout << "Result: " << op(x, y) << std::endl; } int main() { // 使用函数指针调用不同的操作 executeOperation(5, 3, add); // 输出:Result: 8 executeOperation(5, 3, multiply); // 输出:Result: 15 return 0; }

在这个示例中,我们定义了一个函数指针类型Operation,它可以指向任何符合int(int, int)签名的函数。然后,我们定义了两个这样的函数addmultiply。在executeOperation函数中,我们使用函数指针来调用传递进来的具体函数。

示例2:使用std::functionstd::bind

C++11引入了std::functionstd::bind,使得处理函数对象(包括函数指针、lambda表达式和其他可调用对象)更加方便和灵活。

cpp
#include <iostream> #include <functional> // 定义两个符合某个签名的函数 int add(int a, int b) { return a + b; } int multiply(int a, int b) { return a * b; } // 接受std::function作为参数的函数 void executeOperation(int x, int y, std::function<int(int, int)> op) { std::cout << "Result: " << op(x, y) << std::endl; } int main() { // 使用std::function调用不同的操作 executeOperation(5, 3, add); // 输出:Result: 8 executeOperation(5, 3, multiply); // 输出:Result: 15 // 使用lambda表达式 executeOperation(5, 3, [](int a, int b) { return a - b; }); // 输出:Result: 2 // 使用std::bind auto add5 = std::bind(add, std::placeholders::_1, 5); executeOperation(10, 0, add5); // 输出:Result: 15 return 0; }

在这个示例中,我们使用std::function来接受任何符合签名int(int, int)的可调用对象,包括函数指针、lambda表达式和通过std::bind生成的函数对象。这样,代码的灵活性和可读性得到了提升。

总结

通过使用函数指针或者std::function,C++程序可以实现高阶函数,从而实现更灵活和可扩展的设计。使用std::functionstd::bind提供了更多的功能和更好的类型安全性,特别是在C++11及其后的版本中。

====================

 

C++11引入的lambda表达式为C++提供了一种简洁且强大的方式来定义匿名函数。以下是lambda表达式的详细介绍和一些常见的用法示例。

Lambda表达式的基本语法

Lambda表达式的基本语法如下:

cpp
[capture](parameters) -> return_type { // function body }
  • capture:捕获列表,用于指定哪些外部变量可以被lambda表达式捕获。
  • parameters:参数列表,类似于普通函数的参数列表。
  • return_type:返回类型,可以省略,编译器会自动推导。
  • function body:函数体,定义lambda的具体行为。

示例1:基本的Lambda表达式

cpp
#include <iostream> int main() { // 简单的lambda表达式,捕获列表为空 auto add = [](int a, int b) -> int { return a + b; }; std::cout << "5 + 3 = " << add(5, 3) << std::endl; // 输出:5 + 3 = 8 return 0; }

捕获列表

捕获列表用于指定lambda表达式如何捕获外部作用域的变量。常见的捕获方式包括:

  • [a, &b]:按值捕获a,按引用捕获b
  • [=]:按值捕获所有外部作用域的变量。
  • [&]:按引用捕获所有外部作用域的变量。
  • [this]:捕获当前对象的指针。

示例2:捕获列表

cpp
#include <iostream> int main() { int x = 10; int y = 20; // 按值捕获x,按引用捕获y auto add = [x, &y](int a) -> int { return x + y + a; }; y = 30; // 修改y的值 std::cout << "Result: " << add(5) << std::endl; // 输出:Result: 45 return 0; }

在这个示例中,x被按值捕获,因此lambda表达式内部使用的是捕获时x的值(10)。y被按引用捕获,因此lambda表达式内部使用的是y的当前值(30)。

省略返回类型

通常情况下,编译器可以自动推导lambda表达式的返回类型,因此可以省略返回类型声明:

cpp
#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> numbers = {1, 2, 3, 4, 5}; // 使用lambda表达式打印每个元素 std::for_each(numbers.begin(), numbers.end(), [](int n) { std::cout << n << " "; }); std::cout << std::endl; return 0; }

捕获this指针

在类成员函数中,lambda表达式可以捕获this指针,以便访问类的成员变量和成员函数:

cpp
#include <iostream> class Counter { public: Counter(int init) : count(init) {} void increment() { // 捕获this指针,访问类的成员变量 auto inc = [this]() { ++count; }; inc(); } void print() const { std::cout << "Count: " << count << std::endl; } private: int count; }; int main() { Counter counter(0); counter.increment(); counter.print(); // 输出:Count: 1 return 0; }

使用lambda表达式的更多示例

示例3:在标准算法中使用lambda

cpp
#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; // 使用lambda表达式筛选偶数 numbers.erase(std::remove_if(numbers.begin(), numbers.end(), [](int n) { return n % 2 != 0; }), numbers.end()); // 打印结果 std::for_each(numbers.begin(), numbers.end(), [](int n) { std::cout << n << " "; }); std::cout << std::endl; // 输出:2 4 6 8 10 return 0; }

示例4:排序

cpp
#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> numbers = {5, 2, 9, 1, 5, 6}; // 使用lambda表达式进行降序排序 std::sort(numbers.begin(), numbers.end(), [](int a, int b) { return a > b; }); // 打印排序结果 for (int n : numbers) { std::cout << n << " "; } std::cout << std::endl; // 输出:9 6 5 5 2 1 return 0; }

总结

C++11的lambda表达式为编写简洁、高效的代码提供了强大的工具。它们可以捕获外部变量,自动推导返回类型,并且可以与标准库算法很好地结合使用。通过合理利用lambda表达式,可以大大提高代码的可读性和维护性。

 
======================

前面讲完了lambda表达式的基本使用,最后给出lambda表达式的完整语法:

// 完整语法
[ capture-list ] ( params ) mutable(optional) constexpr(optional)(c++17) exception attribute -> ret { body } 

// 可选的简化语法
[ capture-list ] ( params ) -> ret { body }     
[ capture-list ] ( params ) { body }    
[ capture-list ] { body } 

第一个是完整的语法,后面3个是可选的语法。这意味着lambda表达式相当灵活,但是照样有一定的限制,比如你使用了拖尾返回类型,那么就不能省略参数列表,尽管其可能是空的。针对完整的语法,我们对各个部分做一个说明:

  • capture-list:捕捉列表,这个不用多说,前面已经讲过,记住它不能省略;
  • params:参数列表,可以省略(但是后面必须紧跟函数体);
  • mutable:可选,将lambda表达式标记为mutable后,函数体就可以修改传值方式捕获的变量;
  • constexpr:可选,C++17,可以指定lambda表达式是一个常量函数;
  • exception:可选,指定lambda表达式可以抛出的异常;
  • attribute:可选,指定lambda表达式的特性;
  • ret:可选,返回值类型;
  • body:函数执行体。


作者:小白将
链接:https://www.jianshu.com/p/d686ad9de817
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
===========================

C++11的一大亮点就是引入了Lambda表达式。利用Lambda表达式,可以方便的定义和创建匿名函数。对于C++这门语言来说来说,“Lambda表达式”或“匿名函数”这些概念听起来好像很深奥,但很多高级语言在很早以前就已经提供了Lambda表达式的功能,如C#,Python等。今天,我们就来简单介绍一下C++中Lambda表达式的简单使用。

声明Lambda表达式

Lambda表达式完整的声明格式如下:

[capture list] (params list) mutable exception-> return type { function body }

各项具体含义如下

  1. capture list:捕获外部变量列表
  2. params list:形参列表
  3. mutable指示符:用来说用是否可以修改捕获的变量
  4. exception:异常设定
  5. return type:返回类型
  6. function body:函数体

此外,我们还可以省略其中的某些成分来声明“不完整”的Lambda表达式,常见的有以下几种:

序号格式
1 [capture list] (params list) -> return type {function body}
2 [capture list] (params list) {function body}
3 [capture list] {function body}

其中:

  • 格式1声明了const类型的表达式,这种类型的表达式不能修改捕获列表中的值。
  • 格式2省略了返回值类型,但编译器可以根据以下规则推断出Lambda表达式的返回类型: (1):如果function body中存在return语句,则该Lambda表达式的返回类型由return语句的返回类型确定; (2):如果function body中没有return语句,则返回值为void类型。
  • 格式3中省略了参数列表,类似普通函数中的无参函数。

    讲了这么多,我们还没有看到Lambda表达式的庐山真面目,下面我们就举一个实例。

复制代码
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

bool cmp(int a, int b)
{
    return  a < b;
}

int main()
{
    vector<int> myvec{ 3, 2, 5, 7, 3, 2 };
    vector<int> lbvec(myvec);

    sort(myvec.begin(), myvec.end(), cmp); // 旧式做法
    cout << "predicate function:" << endl;
    for (int it : myvec)
        cout << it << ' ';
    cout << endl;

    sort(lbvec.begin(), lbvec.end(), [](int a, int b) -> bool { return a < b; });   // Lambda表达式
    cout << "lambda expression:" << endl;
    for (int it : lbvec)
        cout << it << ' ';
}
复制代码

在C++11之前,我们使用STL的sort函数,需要提供一个谓词函数。如果使用C++11的Lambda表达式,我们只需要传入一个匿名函数即可,方便简洁,而且代码的可读性也比旧式的做法好多了。

下面,我们就重点介绍一下Lambda表达式各项的具体用法。

捕获外部变量

Lambda表达式可以使用其可见范围内的外部变量,但必须明确声明(明确声明哪些外部变量可以被该Lambda表达式使用)。那么,在哪里指定这些外部变量呢?Lambda表达式通过在最前面的方括号[]来明确指明其内部可以访问的外部变量,这一过程也称过Lambda表达式“捕获”了外部变量。

我们通过一个例子来直观地说明一下:

复制代码
#include <iostream>
using namespace std;

int main()
{
    int a = 123;
    auto f = [a] { cout << a << endl; }; 
    f(); // 输出:123

    //或通过“函数体”后面的‘()’传入参数
    auto x = [](int a){cout << a << endl;}(123); 
}
复制代码

上面这个例子先声明了一个整型变量a,然后再创建Lambda表达式,该表达式“捕获”了a变量,这样在Lambda表达式函数体中就可以获得该变量的值。

类似参数传递方式(值传递、引入传递、指针传递),在Lambda表达式中,外部变量的捕获方式也有值捕获、引用捕获、隐式捕获。

1、值捕获

值捕获和参数传递中的值传递类似,被捕获的变量的值在Lambda表达式创建时通过值拷贝的方式传入,因此随后对该变量的修改不会影响影响Lambda表达式中的值。

示例如下:

复制代码
int main()
{
    int a = 123;
    auto f = [a] { cout << a << endl; }; 
    a = 321;
    f(); // 输出:123
}
复制代码

这里需要注意的是,如果以传值方式捕获外部变量,则在Lambda表达式函数体中不能修改该外部变量的值。

2、引用捕获

使用引用捕获一个外部变量,只需要在捕获列表变量前面加上一个引用说明符&。如下:

复制代码
int main()
{
    int a = 123;
    auto f = [&a] { cout << a << endl; }; 
    a = 321;
    f(); // 输出:321
}
复制代码

从示例中可以看出,引用捕获的变量使用的实际上就是该引用所绑定的对象。

3、隐式捕获

上面的值捕获和引用捕获都需要我们在捕获列表中显示列出Lambda表达式中使用的外部变量。除此之外,我们还可以让编译器根据函数体中的代码来推断需要捕获哪些变量,这种方式称之为隐式捕获。隐式捕获有两种方式,分别是[=]和[&]。[=]表示以值捕获的方式捕获外部变量,[&]表示以引用捕获的方式捕获外部变量。

隐式值捕获示例:

复制代码
int main()
{
    int a = 123;
    auto f = [=] { cout << a << endl; };    // 值捕获
    f(); // 输出:123
}
复制代码

隐式引用捕获示例:

复制代码
int main()
{
    int a = 123;
    auto f = [&] { cout << a << endl; };    // 引用捕获
    a = 321;
    f(); // 输出:321
}
复制代码

4、混合方式

上面的例子,要么是值捕获,要么是引用捕获,Lambda表达式还支持混合的方式捕获外部变量,这种方式主要是以上几种捕获方式的组合使用。

到这里,我们来总结一下:C++11中的Lambda表达式捕获外部变量主要有以下形式:

捕获形式说明
[] 不捕获任何外部变量
[变量名, …] 默认以值得形式捕获指定的多个外部变量(用逗号分隔),如果引用捕获,需要显示声明(使用&说明符)
[this] 以值的形式捕获this指针
[=] 以值的形式捕获所有外部变量
[&] 以引用形式捕获所有外部变量
[=, &x] 变量x以引用形式捕获,其余变量以传值形式捕获
[&, x] 变量x以值的形式捕获,其余变量以引用形式捕获

修改捕获变量

前面我们提到过,在Lambda表达式中,如果以传值方式捕获外部变量,则函数体中不能修改该外部变量,否则会引发编译错误。那么有没有办法可以修改值捕获的外部变量呢?这是就需要使用mutable关键字,该关键字用以说明表达式体内的代码可以修改值捕获的变量,示例:

复制代码
int main()
{
    int a = 123;
    auto f = [a]()mutable { cout << ++a; }; // 不会报错
    cout << a << endl; // 输出:123
    f(); // 输出:124
}
复制代码

Lambda表达式的参数

Lambda表达式的参数和普通函数的参数类似,那么这里为什么还要拿出来说一下呢?原因是在Lambda表达式中传递参数还有一些限制,主要有以下几点:

  1. 参数列表中不能有默认参数
  2. 不支持可变参数
  3. 所有参数必须有参数名

常用举例:

复制代码
   {
     int m = [](int x) { return [](int y) { return y * 2; }(x)+6; }(5); std::cout << "m:" << m << std::endl;   //输出m:16 std::cout << "n:" << [](int x, int y) { return x + y; }(5, 4) << std::endl; //输出n:9 auto gFunc = [](int x) -> function<int(int)> { return [=](int y) { return x + y; }; }; auto lFunc = gFunc(4); std::cout << lFunc(5) << std::endl; auto hFunc = [](const function<int(int)>& f, int z) { return f(z) + 1; }; auto a = hFunc(gFunc(7), 8); int a = 111, b = 222; auto func = [=, &b]()mutable { a = 22; b = 333; std::cout << "a:" << a << " b:" << b << std::endl; }; func(); std::cout << "a:" << a << " b:" << b << std::endl; a = 333; auto func2 = [=, &a] { a = 444; std::cout << "a:" << a << " b:" << b << std::endl; }; func2(); auto func3 = [](int x) ->function<int(int)> { return [=](int y) { return x + y; }; };

  
     std::function<void(int x)> f_display_42 = [](int x) { print_num(x); };
	f_display_42(44);
  }
复制代码

 

来自:https://www.cnblogs.com/DswCnblog/p/5629165.html

 ============

c++ 11 std::function 与 std::bind
ChatGPT

C++11引入了两个强大的工具:std::functionstd::bind。它们用于处理和操作可调用对象(如函数、lambda表达式、函数对象等)。下面是它们的详细介绍和一些使用示例。

std::function

std::function是一个通用、多态的函数封装器,可以存储、复制和调用任何可调用的目标(函数、lambda表达式、函数对象、以及指向成员函数的指针)。

示例1:基本使用

cpp
#include <iostream> #include <functional> // 普通函数 int add(int a, int b) { return a + b; } int main() { // 使用std::function封装普通函数 std::function<int(int, int)> func = add; std::cout << "5 + 3 = " << func(5, 3) << std::endl; // 输出:5 + 3 = 8 return 0; }

示例2:封装lambda表达式

cpp
#include <iostream> #include <functional> int main() { // 使用std::function封装lambda表达式 std::function<int(int, int)> func = [](int a, int b) -> int { return a + b; }; std::cout << "5 + 3 = " << func(5, 3) << std::endl; // 输出:5 + 3 = 8 return 0; }

示例3:封装成员函数

cpp
#include <iostream> #include <functional> class Calculator { public: int add(int a, int b) { return a + b; } }; int main() { Calculator calc; // 使用std::function封装成员函数 std::function<int(Calculator&, int, int)> func = &Calculator::add; std::cout << "5 + 3 = " << func(calc, 5, 3) << std::endl; // 输出:5 + 3 = 8 return 0; }

std::bind

std::bind是一个函数适配器,用于将函数和其部分参数绑定在一起,生成一个新的可调用对象。它可以绑定普通函数、成员函数以及std::function对象。

示例4:基本使用

cpp
#include <iostream> #include <functional> // 普通函数 int add(int a, int b) { return a + b; } int main() { // 绑定函数和参数,生成新的可调用对象 auto add_five = std::bind(add, std::placeholders::_1, 5); std::cout << "10 + 5 = " << add_five(10) << std::endl; // 输出:10 + 5 = 15 return 0; }

示例5:绑定成员函数

cpp
#include <iostream> #include <functional> class Calculator { public: int add(int a, int b) { return a + b; } }; int main() { Calculator calc; // 绑定成员函数和对象 auto add_five = std::bind(&Calculator::add, calc, std::placeholders::_1, 5); std::cout << "10 + 5 = " << add_five(10) << std::endl; // 输出:10 + 5 = 15 return 0; }

示例6:结合std::functionstd::bind

cpp
#include <iostream> #include <functional> // 普通函数 int multiply(int a, int b) { return a * b; } int main() { // 使用std::bind绑定部分参数 auto times_ten = std::bind(multiply, std::placeholders::_1, 10); // 使用std::function封装绑定结果 std::function<int(int)> func = times_ten; std::cout << "6 * 10 = " << func(6) << std::endl; // 输出:6 * 10 = 60 return 0; }

总结

  • std::function:是一个通用的函数包装器,可以存储任何类型的可调用对象。它提供了类型安全的方式来操作这些对象,并且可以在需要存储和传递回调函数的场景中使用。
  • std::bind:用于绑定函数及其部分参数,生成一个新的可调用对象。它非常适合用于参数部分已知而部分未知的场景。

通过合理使用这两个工具,C++程序可以更灵活和高效地管理和操作函数和回调。

 

====================

参考:

https://www.cnblogs.com/TenosDoIt/p/3164081.html

chatgpt

posted @ 2024-05-28 10:12  redrobot  阅读(64)  评论(0编辑  收藏  举报