是否有更短的方法来写复合'if'条件? [英] Is there a shorter way to write compound 'if' conditions?

查看:143
本文介绍了是否有更短的方法来写复合'if'条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只需代替

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'
}

这篇关于是否有更短的方法来写复合'if'条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆