迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > C++ >

在 C++ 中创建使用回调函数

作者:迹忆客 最近更新:2023/03/31 浏览次数:

本文将介绍几种在 C++ 中使用回调函数的方法。


在 C++ 中用不同的表示法来声明回调函数

回调是指作为参数传递给其他函数的函数(即代码中的子程序),以便在程序执行中稍后调用。

回调函数可以使用不同的语言专用工具来实现,但在 C++ 中,所有的回调函数都被称为可调用对象。可调用对象可以是传统的函数、函数的指针、lambda 表达式、bind 创建的对象、重载 () 操作符的类以及 <functional> 头中定义的 std::function 类型对象。

下面的示例代码定义了两个传统函数 addTwoInts/subtructTwoInts,一个是存储在 modOfTwoInts1 变量中的 lambda 表达式,一个是 std::function 类型的 modOfTwoInts2。这些函数实现了整数的基本算术运算符+-modulo

#include <iostream>
#include <functional>
#include <vector>
#include <map>

using std::cout; using std::cin;
using std::endl; using std::map;
using std::function; using std::string;

int addTwoInts(int i, int j) { return i + j; }
int subtructTwoInts(int i, int j) { return i - j; }

int main() {
    auto modOfTwoInts1 = [](int i, int j) { return i % j; };
    cout << "modOfTwoInts1(10, 3) = " << modOfTwoInts1(10, 3) << endl;

    cout << "addTwoInts(10, 3) = " << addTwoInts(10, 3) << endl;
    cout << "subtructTwoInts(10, 3) = " << subtructTwoInts(10, 3) << endl;

    function<int (int, int)> modOfTwoInts2 = [](int i, int j) { return i % j; };
    cout << "modOfTwoInts2(10, 3) = " << modOfTwoInts2(10, 3) << endl;

    return EXIT_SUCCESS;
}

输出:

modOfTwoInts1(10, 3) = 1
addTwoInts(10, 3) = 13
subtructTwoInts(10, 3) = 7
modOfTwoInts2(10, 3) = 1

使用 std::map 在 C++ 中存储带有相应键的多个回调函数

使用回调函数的常见方法是将它们存储在 vectormap 这样的数据结构中,我们可以很方便地从这些数据结构中访问其中的每一个函数,并在程序运行时调用特定的函数。在本例中,我们选择了一个 map 容器,将算术运算符作为字符串存储为键,将相应的函数作为值。需要注意的是,这个代码示例并没有向控制台输出任何内容。

#include <iostream>
#include <functional>
#include <map>

using std::cout; using std::cin;
using std::endl; using std::map;
using std::function; using std::string;

int addTwoInts(int i, int j) { return i + j; }
int subtructTwoInts(int i, int j) { return i - j; }

int main() {
    auto modOfTwoInts1 = [](int i, int j) { return i % j; };
    auto subtruct = subtructTwoInts;

    map<string, int(*) (int,int)> op_funcs;
    op_funcs.insert({"+", addTwoInts});
    op_funcs.insert({"%", modOfTwoInts1});
    op_funcs.insert({"-", subtruct});

    return EXIT_SUCCESS;
}

在 C++ 中根据用户输入的内容调用特定的回调函数

由于上一节的原因,应切实利用存储在 map 容器中的回调函数。一种方法是从用户那里获取操作符,并将其传递给 map 容器,作为调用相应函数对象的键。这种方法在 UI 应用中经常被用来处理事件。注意,我们传递的是调用任意整数参数 (10, 3) 的函数。

#include <iostream>
#include <functional>
#include <map>

using std::cout; using std::cin;
using std::endl; using std::map;
using std::function; using std::string;

int addTwoInts(int i, int j) { return i + j; }
int subtructTwoInts(int i, int j) { return i - j; }

int main() {
    auto modOfTwoInts1 = [](int i, int j) { return i % j; };
    auto subtruct = subtructTwoInts;

    map<string, int(*) (int,int)> op_funcs;
    op_funcs.insert({"+", addTwoInts});
    op_funcs.insert({"%", modOfTwoInts1});
    op_funcs.insert({"-", subtruct});

    string user_input;
    cout << "\nType one of the following ops\n"
            "for integers 10 and 3 to be used:\n"
            "1) + 2) - 3) % \n";

    cin >> user_input;
    cout << op_funcs[user_input](10, 3);

    return EXIT_SUCCESS;
}

输出(如果输入是+):

Type one of the following ops
for integers 10 and 3 to be used:
1) + 2) - 3) %
+  
13

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Arduino 中停止循环

发布时间:2024/03/13 浏览次数:444 分类:C++

可以使用 exit(0),无限循环和 Sleep_n0m1 库在 Arduino 中停止循环。

Arduino 复位

发布时间:2024/03/13 浏览次数:315 分类:C++

可以通过使用复位按钮,Softwarereset 库和 Adafruit SleepyDog 库来复位 Arduino。

Arduino 的字符转换为整型

发布时间:2024/03/13 浏览次数:181 分类:C++

可以使用简单的方法 toInt()函数和 Serial.parseInt()函数将 char 转换为 int。

Arduino 串口打印多个变量

发布时间:2024/03/13 浏览次数:381 分类:C++

可以使用 Serial.print()和 Serial.println()函数在串口监视器上显示变量值。

Arduino if 语句

发布时间:2024/03/13 浏览次数:123 分类:C++

可以使用 if 语句检查 Arduino 中的不同条件。

Arduino ICSP

发布时间:2024/03/13 浏览次数:214 分类:C++

ICSP 引脚用于两个 Arduino 之间的通信以及对 Arduino 引导加载程序进行编程。

使用 C++ 编程 Arduino

发布时间:2024/03/13 浏览次数:127 分类:C++

本教程将讨论使用 Arduino IDE 在 C++ 中对 Arduino 进行编程。

Arduino 中的子程序

发布时间:2024/03/13 浏览次数:168 分类:C++

可以通过在 Arduino 中声明函数来处理子程序。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便