基于标志运行两个不同的函数
Run two different functions based on flags?
我正在编写一个程序,用户可以在其中设置选项标志。
我的问题是我想根据用户标志调用函数的不同版本,并且我不想每次选择要调用的版本时都if(flag)
,因为我必须在我处理的每一行上检查if()
语句。
这是一个学校项目,所以我试图找到最有效的方法来确定我想调用哪个函数, and I read
if()' 语句在这里很昂贵。
那么有没有办法在开头基本上说
if(flag)
// use this function the rest of the program
else
// use this other function for the rest of the program
因此,如果有一种方法可以在开始时说if(flag)使用此函数,则程序的其余部分将其他函数用于程序的其余部分
只需执行以下操作:
void func1() {
// ...
}
void func2() {
// ...
}
int main(int argc, char* argv[]) {
std::function<void()> func;
// Make your decision once:
if(std::string(argv[1]) == "func1") {
func = func1;
}
else if(std::string(argv[1]) == "func2") {
func = func2;
}
// Call func many times
func();
// ...
}
您可以使用旧样式的函数指针数组,如下所示:
#include <stdio.h>
int sum(int a, int b);
int subtract(int a, int b);
int mul(int a, int b);
int div(int a, int b);
int (*p[4]) (int x, int y);
typedef enum{
SUM=0,
SUB,
MUL,
DIV
}FLAG;
int main(void)
{
int result;
int i, j, op;
p[SUM] = sum; /* address of sum() */
p[SUB] = subtract; /* address of subtract() */
p[MUL] = mul; /* address of mul() */
p[DIV] = div; /* address of div() */
printf("Enter two numbers: ");
scanf("%d %d", &i, &j);
printf("0: Add, 1: Subtract, 2: Multiply, 3: Dividen");
do {
printf("Enter number of operation: ");
scanf("%d", &op);
} while(op<0 || op>3);
result = (*p[op]) (i, j);
printf("%d", result);
return 0;
}
int sum(int a, int b)
{
return a + b;
}
int subtract(int a, int b)
{
return a - b;
}
int mul(int a, int b)
{
return a * b;
}
int div(int a, int b)
{
if(b)
return a / b;
else
return 0;
}
做这种事情的最好方法是使用模板。您的问题可以理解为类似于"环路拉出"问题:
void foo(vector v, bool b) {
for (auto e : v) {
// do stuff
if (b) // stuff
else // other stuff
// do more stuff
}
}
分支在循环过程中不会改变,但我们一直在点击它。优雅地重写它非常困难,特别是如果分支前后有一堆代码。使用模板,我们可以使用这种方法:
template <bool b>
void foo(vector<double>& v) {
for (auto e : v) {
// do stuff
if (b) // stuff
else // other stuff
// do more stuff
}
}
void foo(vector<double>& v, bool b) {
if (b) foo<true>(v);
else foo<false>(v);
}
在内部编译器将生成两个版本的foo
,一个版本的布尔值在编译时被硬编码为true,另一个在编译时被硬编码为false。在每种情况下,编译器都可以毫无问题地完全消除分支。然后,您只需在调用开始时调度到正确的函数,仅分支一次。但是代码的结构完全相同;您只需添加几行代码,这些代码不会随着循环变得更大而增加。
相关文章:
- 如何巧妙地编写两个函数——一个用于检查是否存在解决方案,另一个用于获取所有解决方案
- 两个函数模板候选项.将一个参数作为引用后,选择不太专业的模板
- 如何比较两个函数的速度和性能
- 如何在构造器的成员初始值设定项列表中调用两个函数?
- 如何将这两个函数组合成一个实现?
- 将具有相反操作数的两个函数重构为一个
- 创建整数的 2D 数组,该数组将使用两个函数用随机数填充矩阵.我做错了什么?
- 具有相同主体的两个函数具有不同的名称
- 通过作为类的公共成员的两个函数将函数作为参数传递
- 如何使用排序和比较这两个函数在 c++ 中对字符数组进行排序?
- 是否可以在C++中将两个函数一起添加
- C++ 两个函数除了一行之外执行相同的操作
- 如何编写连接两个函数的函数
- 如何执行两个函数参数具有相同的模板类型
- 当两个函数位于一行中时,堆栈框架的样子
- 为什么这两个函数的行为不同
- 为什么当我有两个函数时编译器没有显示错误,一个将采用基类,一个将派生类作为参数
- 系统堆栈中的两个函数的递归调用(将不同数量的数组作为参数传递)有什么区别
- 如何在一个构造函数中使用两个可变参数模板参数来绑定两个函数
- 如何在c++中的两个函数中传递对象的同一实例