如何缩短多重&&&条件
How to shorten multiple && condition
目前我可以运行我的程序,但我的代码由很多重复组成,看起来像:
while(option != 'E' && option != 'D' && option != 'P' && option != 'Q' &&
option != 'e' && option != 'd' && option != 'p' && option != 'q') {
// Some code here
}
或:
while(cType != 'S' && cType != 'L' && cType != 'O' && cType != 'Q' &&
cType != 's' && cType != 'l' && cType != 'o' && cType != 'q') {
// Some code here
}
缩短以上代码的最快方法是什么?
const char invalidChars[] = "edpq";
while (strchr(invalidChars, tolower(option)) != 0) {
...
}
您可以初始化一个包含您想要匹配的字符的字符串,然后使用find
,如果没有找到匹配,则返回npos
:
string match = "SLOQsloq";
while (match.find(cType) == string::npos)
...
使用std::tolower
可以消除一半的条件
while(std::tolower(option) != 'e' && std::tolower(option) != 'd' && std::tolower(option) != 'p' && std::tolower(option) != 'q')
您也可以使用std::string
及其find
成员函数,如:
std::string options = "edpq";
//...
while (options.find(std::tolower(option)) == std::string::npos)
可以通过使用std::set
并检查集合是否包含(或不包含)我们正在比较的变量来简化逻辑并使内容更易于阅读:
std::set<char> someChars { 'a', 'b', 'c' };
if(someChars.find(myChar) != someChars.end()) {
// myChar is either 'a', 'b', or 'c'
}
在大多数其他语言中,这个条件会写得更干净,比如someChars.contains(myChar)
(但是c++的set接口非常小)。
但是,对于少量的比较,您的方法可能更快。
一个没有分支的候选快速测试。
1)无大小写。
2)形成4个差的乘积。
3) 1与0比较
只接受字母。
最多4个(sizeof int/sizeof char
)字母。
大小写相差相同位。(适用于ASCII和EBCDIC)
#define CaseMaskBits ((unsigned char)~('A'^'a'))
#define Product4(ch, s) ((ch-s[0]) * (ch-s[1]) * (ch-s[2]) * (ch-s[3]))
#define TestEq4(ch, t, s) (t=ch&CaseMaskBits, !Product4(t, s))
int main(void) {
int ch;
printf("%Xn", CaseMaskBits);
for (ch = 0; ch < 256; ch++){
int t; // temp var for TestEQ4
while (TestEq4(ch, t, "ELPQ")) {
printf("%d %cn", ch, ch);
break;
}
}
return 0;
}
280 while (TestEq4(ch, t, "ELPQ")) {
00402560: mov %ebx,%eax
00402562: and $0xdf,%eax
00402567: lea -0x4c(%eax),%edx
0040256a: lea -0x45(%eax),%ecx
0040256d: imul %edx,%ecx
00402570: lea -0x50(%eax),%edx
00402573: sub $0x51,%eax
00402576: imul %ecx,%edx
00402579: imul %edx,%eax
0040257c: test %eax,%eax
0040257e: jne 0x402555 <main+37>
281 printf("%d %cn", ch, ch);
如果option/cType
有<= 8位的重要性,为了速度,使用表查找。@Dieter高年级队
unsigned char Table[UCHAR_MAX + 1] = {
fill per needs };
#define Table_OptionMask 1
#define Table_cTypeMask 2
#define Table_nextMask 4
while (!(Table[(unsigned char)option] & Table_OptionMask)) ...
while (!(Table[(unsigned char)cType] & Table_cTypeMask)) ...
为了简化代码维护,在代码启动时调用Table_Setup()
填充表。
static void Table_SetInsensitive(unsigned char *Table, unsigned mask, cnst char *src) {
while (*src) {
Table[toupper((unsigned char) *src)] |= mask;
Table[tolower((unsigned char) *src)] |= mask;
src++;
}
}
void Table_Setup(void) {
memset(Table, 0, sizeof Table);
Table_SetInsensitive(Table, Table_OptionMask, "EDPQ");
Table_SetInsensitive(Table, Table_cTypeMask, "SLOQ");
Table_SetInsensitive(Table, Table_cTypeMask, tbd);
}
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- 在没有太多条件句的情况下,我如何避免被零除
- 基于多个条件处理地图中的所有元素
- 条件constexpr函数
- 无论条件是否为true,if总是在c++中执行
- 函数何时会在c++中包含stack_Unwind_Resume调用
- 我可以使用条件运算符初始化C风格的字符串文字吗
- 基于模板值的条件变量
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- Python中的for循环与C++有何不同
- 将按位if条件转换为普通if条件
- 条件断点在不应该触发时触发
- 为什么简单的算术减法在"if"条件下不起作用?
- 如何在for循环中包含两个索引值的测试条件
- 如果条件为TRUE(最佳方式?),则在do while循环中后置增量
- 如何使用循环缩短条件
- 缩短条件以检查x是否不是四个数字中的任何一个
- 如何缩短多重&&&条件
- 我如何缩短检查子字符串存在的多个条件
- if语句-缩短C++if条件