我知道这已经讨论过,并且有多个答案.例如,请参阅if和switch语句的函数数组的性能,但是我想得到一些其他的想法.
我有一个带有大量switch声明的函数.这是26 case,每个都有"左"或"右"选项.此函数根据两个给定参数(plane和direction)返回指针:
double* getPointer(int plane, int direction) {
switch (plane)
{
case 0:
if (direction == 0)
return p_YZ_L; // Left
else if (dir == 1)
return p_YZ_R; //Right
else {
return 0;
}
break;
...
case 25:
...
}
}
Run Code Online (Sandbox Code Playgroud)
同
planes -> [0-25]
direction -> [0,1]
Run Code Online (Sandbox Code Playgroud)
我一直在考虑一系列功能,但这也可能很乏味,我不确定它是否是最好的选择.我还不清楚如何正确地做到这一点.有任何想法吗?
dbu*_*ush 10
您可以像这样创建一个查找表:
double *pointers[26][2] = {
{ p_YZ_L, p_YZ_R },
...
};
Run Code Online (Sandbox Code Playgroud)
然后你的功能变得更简单:
double* getPointer(int plane, int direction) {
if ((plane >= 0) && (plane < 26) && (direction >= 0) && (direction < 2)) {
return pointers[plane][direction];
} else {
return NULL;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2127 次 |
| 最近记录: |