多个字符比较inswitch声明 [英] multiple charecter comparison inswitch statement

查看:96
本文介绍了多个字符比较inswitch声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,



我对switch语句有一点疑问。

我们可以使用switch语句进行比较吗?一个字符,例如:

Hey guys,

I have a small doubt regarding the switch statement.
can we use the switch statement to compare more than one character, for example :

switch(I do not know which data type to use)
{
       case 'hello':
                { cout << " you typed hello ";break;} 
}

推荐答案

你不能。

switch语句中的表达式必须是一个整数,或一个可以明确转换为整数的类型。字符串值不能,因为它可以超过整数可以容纳。
You can't.
The expression in a switch statement must be an integer, or a type which can be unambiguously converted to an integer. A string value cannot, because it can by longer than an integer can hold.


这是多字符文字,它的计算结果为整数类型的值。整数类型的宽度不超过int类型的宽度。如果您正在编写与平台无关的代码,我不建议使用多字符文字。为什么? http://stackoverflow.com/questions/3960954/c-multicharacter-literal [ ^ ]

你可以解决同样的问题没有使用多字符文字的十几种其他方法的问题。
This is a multicharacter literal and it evaluates to a value of an integral type. The width of the integral type doesn't exceed that of the int type. I don't recommend using multicharacter literals if you are writing platform independent code. Why? http://stackoverflow.com/questions/3960954/c-multicharacter-literal[^]
You can solve the same problem in a dozen other ways without using multicharacter literals.


这篇关于多个字符比较inswitch声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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