是否有更短的方法来编写复合“if"条件?

2021-12-12 00:00:00 c if-statement c++

只是代替:

if  ( ch == 'A' || ch == 'B' || ch == 'C' || .....

例如,做喜欢:

if  ( ch == 'A', 'B', 'C', ...

是否有一种更短的方法来总结条件?

is there even a shorter way to summarize conditions?

推荐答案

strchr() 可用于查看字符是否在列表中.

strchr() can be used to see if the character is in a list.

const char* list = "ABCXZ";
if (strchr(list, ch)) {
  // 'ch' is 'A', 'B', 'C', 'X', or 'Z'
}

相关文章