使用map + function 的方式替代if-else有以下好处:
-
代码更简洁:使用map + function可以将多个if-else语句转化为一行代码,使得代码看起来更加简洁易懂。
-
可读性更好:使用map + function可以将判断逻辑抽象成函数,让代码更具可读性和可维护性。
-
易于扩展:当需要添加新的判断条件时,只需要在函数中增加新的分支即可,而不需要修改大量的if-else语句。
-
更高的性能:使用map + function可以利用函数式编程的特性,让代码更加高效。相比较于if-else语句,map + function可以更快地进行处理。
总之,使用map + function的方式可以让代码更加简洁、易读、易扩展,并且具有更高的性能表现。
#include <iostream>
#include <map>
using namespace std;
// 定义函数指针类型
typedef int (*funcPtr)(int);
// 定义函数实现
int func1(int x) {
return x * 2;
}
int func2(int x) {
return x + 10;
}
int func3(int x) {
return x - 5;
}
int main() {
// 初始化一个map,键为整数,值为相应的函数指针
map<int, funcPtr> myMap = {
{
1, &func1}, {
2, &func2}, {
3, &func3}};
// 测试输入值和对应的函数编号
int x = 5;
int funcNum = 1;
// 使用map和函数指针调用相应的函数
int result = (*myMap[funcNum])(x);
cout << "The result is " << result << endl;
return 0;
}
#include <iostream>
#include <map>
using namespace std;
// 定义函数指针类型
typedef int (*funcPtr)(int);
// 定义函数实现
int func1(int x) {
return x * 2;
}
int func2(int x) {
return x + 10;
}
int func3(int x) {
return x - 5;
}
int main() {
// 初始化一个map,键为整数,值为相应的函数指针
map<int, funcPtr> myMap = {
{
1, &func1}, {
2, &func2}, {
3, &func3}};
// 测试输入值和对应的函数编号
int x = 5;
int funcNum = 1;
// 使用map和函数指针调用相应的函数
int result = (*myMap[funcNum])(x);
cout << "The result is " << result << endl;
return 0;
}
在这个示例代码中,我们首先定义了三个函数func1、func2和func3来模拟不同的处理逻辑。然后我们使用一个map来将输入值和相应的函数指针关联起来。最后,我们根据输入的函数编号从map中取出相应的函数指针,并通过指针调用相应的函数来得到结果。
通过使用map + function的方式,我们可以避免使用大量的if-else语句,在代码简洁性、可读性、可维护性和性能方面都表现较好。
整理内容
C++中的映射是用于存储键值对的非常有用的数据结构。映射是存储的最佳方式,我们可以使用具有最佳时间复杂度的键直接查找和访问值。
但是,您是否知道您也可以将函数存储为C++映射中的值?没错 - 可以使用映射来存储和快速访问需要在程序中不同时间调用的一组函数。让我们检查一下函数图。
C++中的映射是用于存储键值对的非常有用的数据结构。映射是存储的最佳方式,我们可以使用具有最佳时间复杂度的键直接查找和访问值。
但是,您是否知道您也可以将函数存储为C++映射中的值?没错 - 可以使用映射来存储和快速访问需要在程序中不同时间调用的一组函数。让我们检查一下函数图。
语法:
std::function<return_type(arg_type1, arg_type2, ...)> var_name;
在此代码中,return_type是函数的返回类型,arg_type1、_arg_type2_等。是函数参数的类型。
要创建函数映射,我们首先需要定义要使用的函数签名。在这种情况下,我们希望存储接受两个长参数并返回长值的函数。我们可以使用函数头文件中的函数模板来定义一个可以存储此类函数的变量:
// first create a function template
using func=function<long(long,long)>;
// create a map object
unordered_map<string,func> mp{
{
"+", plus<long>()},
{
"-", minus<long>()},
{
"/", divides<long>()},
{
"*", multiplies<long>()}
};
在此代码中,函数加、减、除和乘分别使用字符串键 “+”、“-”、“/”和“ * ”添加到映射对象映射中。
若要调用存储在映射中的函数,可以使用数组访问运算符 [] 检索函数指针,并使用函数调用运算符 () 调用函数。
例如:
long result = map["+"](2, 3); // 5
long result1 = map["-"](2, 3); // -1
long result2= map["*"](2, 3); // 6
long result3 = map["/"](6, 3); // 2
使用映射存储函数有几个优点。它允许您在运行时轻松添加或删除函数,还允许您轻松地将名称与每个函数相关联,这样可以更轻松地理解和维护代码。让我们用一个问题来理解它。
问题陈述:
您已经获得了一个称为标记的字符串数组,这些字符串表示用反向波兰表示法 (RPN) 编写的算术表达式。您的任务是计算此表达式并以整数形式返回结果。每个操作数可以是整数或其他 RPN 表达式。当除以两个整数时,结果将始终被截断为零。您可以假设输入表示有效的 RPN 表达式,并且不会有任何除以零。
让我们检查一些测试用例以了解。
案例1:
Input: ["2", "1", "+", "3", "*"]
Output: 9
Explanation: ((2 + 1) * 3) = 9
案例2:
Input: ["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"]
Output: 22
Explanation: ((10 * (6 / ((9 + 3) * -11))) + 17) + 5 = 22
例:
// C++ Program to implement
// map of functions
#include <bits/stdc++.h>
using namespace std;
// Define a type alias for a function that takes two longs
// and returns a long
using func = function<long(long, long)>;
// Create a map that maps strings to functions
map<string, func> mp{
// Plus function from functools
{
"+", plus<long>() },
// Minus function from functools
{
"-", minus<long>() },
// Divides function from functools
{
"/", divides<long>() },
// Multiplies function from functools
{
"*", multiplies<long>() }
};
// Function to evaluate a Reverse Polish Notation (RPN)
// expression
int evalRPN(vector<string>& A)
{
// Create a stack to store the operands
stack<long> s;
// Iterate over the elements in the RPN expression
for (auto i : A) {
// If the element is not an operator, it is an
// operand Convert it to an integer and push it onto
// the stack
if (!mp[i]) {
s.push(stoi(i));
}
// If the element is an operator, pop the top two
// operands from the stack Perform the operation and
// push the result back onto the stack
else {
auto num1 = s.top();
s.pop();
auto num2 = s.top();
s.pop();
s.push(mp[i](num2, num1));
}
}
// Return the result, which is the top element of the
// stack
return s.top();
}
int main()
{
// Test the evalRPN function with an RPN expression
vector<string> A
= {
"10", "6", "9", "3", "+", "-11", "*",
"/", "*", "17", "+", "5", "+" };
int answer = evalRPN(A);
cout << answer << endl;
return 0;
}
时间复杂度:O(N) // 这里 N 是向量 A 的大小;
**辅助空间:**O(N)。