对C++中function和bind用法一窍不通?别担心,看完这篇文章就搞定了!

一、简介

在设计回调函数的时候,无可避免地会接触到可回调对象。在C++11中,提供了std::function和std::bind两个方法来对可回调对象进行统一和封装。

C++语言中有几种可调用对象:函数、函数指针、lambda表达式、bind创建的对象以及重载了函数调用运算符的类。 和其他对象一样,可调用对象也有类型。例如,每个lambda有它自己唯一的(未命名)类类型;函数及函数指针的类型则由其返回值类型和实参类型决定。
在这里插入图片描述

二、std::function的用法

类似于c语言的函数指针,包含在头文件#include<functional>中。

2.1、保存普通函数

#include <iostream>
#include <functional>
void printA(int a)
{
        std::cout<<"a = "<<a<<std::endl;
}

int main()
{
        std::function<void(int a)> func;
        func=printA;
        func(22);
        return 0;
}

2.2、保存lambda表达式

#include <iostream>
#include <functional>

int main()
{
        std::function<void()> func2=[](){std::cout<<"hello world"<<std::endl;};
        func2();
        return 0;
}

2.3、保存成员函数

#include <iostream>
#include <functional>
class Foo{
public:
        Foo(int num) : num_(num){}
        void print_add(int a) const
        {
                std::cout<<num_+a<<std::endl;
        }
        int num_;
};

int main()
{
        std::function<void(const Foo&,int)> test=&Foo::print_add;
        Foo foo(10);
        test(foo,1);
        return 0;
}

注意,这里的function无法保存重载的成员函数,需要使用std::bind才行。下面的代码编译时会报错:

#include <iostream>
#include <functional>
#include <string>

class Foo{
public:
        Foo(int num) : num_(num){}
        void print_add(int a) const
        {
                std::cout<<num_+a<<std::endl;
        }
        void print_add(std::string c) const
        {
                 std::cout<<c<<std::endl;
        }


        int num_;
};

int main()
{
        std::function<void(const Foo&,int)> test=&Foo::print_add;
        Foo foo(10);
        test(foo,1);
        return 0;
}

编译报错:

function_test.cc: In function ‘int main()’:
function_test.cc:32:50: error: conversion from ‘<unresolved overloaded function type>’ to non-scalar type ‘std::function<void(const Foo&, int)>’ requested
   32 |         std::function<void(const Foo&,int)> test=&Foo::print_add;
      |                                                  ^~~~~~~~~~~~~~~

三、std::bind用法

可将bind函数看作是一个通用的函数适配器,它接受一个可调用对象,生成一个新的可调用对象来“适应”原对象的参数列表。
调用bind的一般形式:

auto newCallable = bind(callable,arg_list);  

其中,newCallable本身是一个可调用对象,arg_list是一个逗号分隔的参数列表,对应给定的callable的参数。即,当我们调用newCallable时,newCallable会调用callable,并传给它arg_list中的参数。

arg_list中的参数可能包含形如n的名字,其中n是一个整数,这些参数是“占位符”,表示newCallable的参数,它们占据了传递给newCallable的参数的“位置”。数值n表示生成的可调用对象中参数的位置:1为newCallable的第一个参数,_2为第二个参数,以此类推。

std::function<void(int,int)> fc = std::bind(&A::fun_3, a,std::placeholders::_1,std::placeholders::_2);

总结起来就是:

  1. 只是绑定函数,参数需要我们自己传入。
  2. 绑定函数时候,也把参数绑定了,直接运行就行。即调用函数时参数已经被固定了,再次修改是没有用的。
  3. bind的目的:不同的任务能传递不同的参数。

使用示例:

#include <iostream>
#include <functional>
#include <string>
using namespace std;

class A{
public:
        void fun_3(int a,int b)
        {
                cout<<"func_3 print: a = "<<a<<", b = "<<b<<endl;
        }
        void fun_4(string str,int b)
        {
                cout<<"func_4 print: str = "<<str<<", b = "<<b<<endl;
        }
        void fun_4(int a,int b)
        {
                cout<<"func_4 print: a = "<<a<<", b = "<<b<<endl;
        }

};

void fun_1(int x,int y,int z)
{
        cout<<"func_1 print: x = "<<x<<", y = "<<y<<", z = "<<z<<endl;
}
void fun_2(int &a,int &b)
{
        a++;
        b++;
        cout<<"func_2 print: a=" <<a<<",b="<<b<<endl;
} 

int main(int argc,char** argv)
{
        // f1的类型为 function<void(int, int, int)>
        auto f1=std::bind(fun_1,1,2,3);//表示绑定函数 fun 的第一,二,三个参数值为: 1 2 3
        f1();
        f1(4,5,6);//修改无效
        cout<<"-----------------------------"<<endl;

        // 占位符
        // 表示绑定函数 fun 的第三个参数为 3,而fun 的第一,二个参数分别由调用 f2 的第一,二个参数 指定
        auto f2=bind(fun_1,placeholders::_1,placeholders::_2,3);
        f2(11,22);
        f2(11,22,33);//修改第三个参数无效
        cout<<"-----------------------------"<<endl;

        // 表示绑定函数 fun 的第三个参数为 3,而fun 的第一,二个参数分别由调用 f3 的第二,一个参数 指定 
        // 注意: f2 和 f3 的区别。
        auto f3=bind(fun_1,placeholders::_2,placeholders::_1,3);
        f3(11,22);
        f3(11,22,33);//修改第三个参数无效
        cout<<"-----------------------------"<<endl;

        // 传入引用
        int a=2,b=3;
        //表示绑定fun_2的第一个参数为b, fun_2的第二个参数由调用f4的第一个参数(_1)指定。
        auto f4=bind(fun_2,placeholders::_1,b);
        f4(a);
        cout<<"main print: a=" <<a<<",b="<<b<<endl;//说明:bind对于不事先绑定的参数,通过std::placeholders传 递的参数是通过引用传递的
        cout<<"-----------------------------"<<endl;

        // 绑定成员函数
        A aa;
        auto f5=bind(&A::fun_3,aa,placeholders::_1,placeholders::_2);
        f5(10,20);
        // std::function<void(int,int)> fc = std::bind(&A::fun_3, a,std::placeholders::_1,std::placeholders::_2); 
        // fc(10,20); //调用a.fun_3,和上面的auto等价
        cout<<"-----------------------------"<<endl;

        // 重载函数
        auto my=bind((void(A::*)(string,int))&A::fun_4,aa,placeholders::_1,13);
        my("hello world");

        return 0;
}

注意,重载成员函数的绑定是需要显式的指出被绑定成员函数的类型。如下所示:

std::function<void(int, char*)> foo_test=std::bind((void (Foo::*)(int,char*))&Foo::foo, 
			 &foo, 
             std::placeholders::_1, 
             std::placeholders::_2);

总结

  1. function保存函数的三个方式:保存普通函数、保存lambda表达式、保存成员函数;但是重载的成员函数需要配合bind才可以。
  2. bind的目的:不同的任务能传递不同的参数。

在这里插入图片描述

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
C语言的语法分析器一般使用自顶向下的递归下降分析法,可以使用LL算法进行实现。下面是一个简单的C语言语法分析器的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #define MAX_TOKEN_LEN 128 enum TokenKind { TK_RESERVED, // Keywords or punctuators TK_IDENT, // Identifiers TK_NUM, // Numeric literals TK_EOF, // End-of-file markers }; typedef struct Token Token; struct Token { enum TokenKind kind; // Token kind Token *next; // Next token int val; // If kind is TK_NUM, its value char *str; // Token string }; // Input program char *user_input; // Current token Token *token; // Function to report an error void error(char *fmt, ...) { va_list ap; va_start(ap, fmt); vfprintf(stderr, fmt, ap); fprintf(stderr, "\n"); exit(1); } // Function to report the current token void error_at(char *loc, char *fmt, ...) { va_list ap; va_start(ap, fmt); int pos = loc - user_input; fprintf(stderr, "%s\n", user_input); fprintf(stderr, "%*s", pos, ""); // Print pos spaces fprintf(stderr, "^ "); vfprintf(stderr, fmt, ap); fprintf(stderr, "\n"); exit(1); } // Function to create a new token Token *new_token(enum TokenKind kind, Token *cur, char *str, int len) { Token *tok = calloc(1, sizeof(Token)); tok->kind = kind; tok->str = strndup(str, len); cur->next = tok; return tok; } // Function to tokenize the input program Token *tokenize() { char *p = user_input; Token head; head.next = NULL; Token *cur = &head; while (*p) { // Skip whitespace characters if (isspace(*p)) { p++; continue; } // Keywords or punctuators if (strncmp(p, "==", 2) == 0 || strncmp(p, "!=", 2) == 0 || strncmp(p, "<=", 2) == 0 || strncmp(p, ">=", 2) == 0) { cur = new_token(TK_RESERVED, cur, p, 2); p += 2; continue; } if (*p == '+' || *p == '-' || *p == '*' || *p == '/' || *p == '(' || *p == ')' || *p == '<' || *p == '>' || *p == ';' || *p == '=') { cur = new_token(TK_RESERVED, cur, p++, 1); continue; } // Numeric literals if (isdigit(*p)) { cur = new_token(TK_NUM, cur, p, 0); char *q = p; cur->val = strtol(p, &p, 10); cur->str = strndup(q, p - q); continue; } // Identifiers if (isalpha(*p) || *p == '_') { cur = new_token(TK_IDENT, cur, p++, 1); while (isalnum(*p) || *p == '_') cur->str = realloc(cur->str, ++cur->len); continue; } // Invalid character error_at(p, "Invalid character"); } new_token(TK_EOF, cur, p, 0); return head.next; } // Function to consume the current token if it matches the specified kind int consume(enum TokenKind kind) { if (token->kind != kind) return 0; token = token->next; return 1; } // Function to expect the current token to be the specified kind void expect(enum TokenKind kind) { if (token->kind != kind) error_at(token->str, "Expected %c", kind); token = token->next; } // Function to expect the current token to be a number and return its value int expect_number() { if (token->kind != TK_NUM) error_at(token->str, "Expected a number"); int val = token->val; token = token->next; return val; } // Function to parse primary expressions int primary() { // If the current token is a number, return its value if (consume(TK_NUM)) return expect_number(); // If the current token is '(', parse an expression inside it if (consume('(')) { int val = expr(); expect(')'); return val; } // Otherwise, it must be a variable error_at(token->str, "Expected a primary expression"); } // Function to parse multiplicative expressions int mul() { int val = primary(); for (;;) { if (consume('*')) val *= primary(); else if (consume('/')) val /= primary(); else return val; } } // Function to parse additive expressions int expr() { int val = mul(); for (;;) { if (consume('+')) val += mul(); else if (consume('-')) val -= mul(); else return val; } } int main(int argc, char **argv) { if (argc != 2) error("Usage: %s <code>", argv[0]); user_input = argv[1]; token = tokenize(); int val = expr(); printf("%d\n", val); return 0; } ``` 这个示例代码只能处理加减乘除和括号,还有很多C语言的语法特性没有考虑到,需要根据需要进行扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Lion Long

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

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

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

打赏作者

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

抵扣说明:

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

余额充值