资讯类网站建设,文山做网站,万户网络科技有限公司怎么样,去掉自动升级wordpress失败提示一、问题背景
开发过程中遇到了需要根据const字符串调用不同函数的要求。在开发过程中为了快速实现功能#xff0c;实际使用了if else等判断实现了不同函数的调用#xff0c;徒增了不少代码行数。
明知道可以采用map管理函数指针#xff0c;但是没有具体实现过#xff0c…一、问题背景
开发过程中遇到了需要根据const字符串调用不同函数的要求。在开发过程中为了快速实现功能实际使用了if else等判断实现了不同函数的调用徒增了不少代码行数。
明知道可以采用map管理函数指针但是没有具体实现过所以不敢贸然采用。为避免凑代码行数之嫌决定使用这种方式去更新写法现将学习过程记录下来。
二、std::map管理函数指针
使用 std::map或其他关联容器如 std::unordered_map来管理 function 函数指针可以为程序设计带来许多优势尤其在需要动态管理和调用函数的场景中。具体包括以下优点
动态映射与查找使用 std::map 可以将特定的标识符如 std::string、int 等与函数指针或 std::function 关联起来使得程序能够根据需求在运行时动态查找并调用函数而无需编写复杂的 switch-case 或 if-else 语句。简化代码结构易于扩展当有新的功能需要添加时只需向 std::map 中插入新的函数指针即可而不必更改原有的代码逻辑。这样可以极大地提高代码的可维护性和扩展性。如果将来需要替换或修改某个功能只需更新映射中的函数指针即可。高效查询与调用std::map 和 std::unordered_map 提供了高效的查找功能。std::map 是一个基于红黑树实现的有序容器查找时间复杂度为 O(log n)而 std::unordered_map 是基于哈希表实现的无序容器查找时间复杂度为平均 O(1)。使用这些容器管理函数指针程序可以快速找到并执行目标函数。
三、std::function 1std::function定义
类模板std::function 是 C 标准库中的一个通用多态函数包装器位于 functional 头文件中。它可以用来存储、复制和调用任何符合特定函数签名的可调用对象比如普通函数、lambda 表达式、std::bind 生成的函数对象或者是函数对象类的实例。
存储的可调用对象称为std::function的目标。如果std::function不包含目标则称为空函数。调用空的std::function的目标函数会产生std::bad_function_call异常。
2std::function语法
#include functional
std::functionReturnType(ArgTypes...)ReturnType指定函数的返回类型。
ArgTypes…指定函数的参数类型列表。
3std::function 与传统函数指针的区别
std::function 是 C11 引入的一个模板类它可以存储、复制和调用任何可调用对象包括函数指针、lambda 表达式、函数对象等。而传统函数指针则只能指向函数。
相较于传统函数指针std::function具有更强的类型安全性但性能上略低于传统函数指针。
四、具体实现
实现步骤如下
采用别名FunctionPtr代替std::functionvoid(int, int)。定义具名函数void func1(int, int)和void func2(int, int)分别实现加法和乘法。定义匿名函数void(int, int)实现减法。定义std::mapstd::string, std::shared_ptrFunctionPtr functionMap;用于管理上述函数。此时functionMap能够根据传入的string不同调用不同的函数。
#include iostream
#include map
#include memory
#include functionalusing FunctionPtr std::functionvoid(int, int);void func1(int x, int y) {std::cout In func1, x y x y std::endl;
}void func2(int x, int y) {std::cout In func2, x * y x * y std::endl;
}int main() {std::mapstd::string, std::shared_ptrFunctionPtr functionMap;functionMap[func1] std::make_sharedFunctionPtr(func1);functionMap[func2] std::make_sharedFunctionPtr(func2);functionMap[func3] std::make_sharedFunctionPtr([](int x, int y) - void {std::cout In func3, x - y x - y std::endl;});for (const auto functionPair : functionMap) {auto funcName functionPair.first;std::cout In main, funcName funcName std::endl;auto function functionPair.second;if (function ! nullptr) {(*function)(1,2);}}return 0;
}五、参考文献
Cpp Reference之std::maphttps://en.cppreference.com/w/cpp/container/map。Cpp Reference之std::functionhttps://en.cppreference.com/w/cpp/utility/functional/function