简体   繁体   English

在开关盒中,char和int有什么区别?

[英]What's the difference between char and int in a switch case?

I started C++ recently and while learning switch case, I got this doubt. 我最近开始使用C ++,在学习切换案例时,我对此表示怀疑。
What's the difference if I use int or char in the following code : 如果在以下代码中使用int或char有什么区别:
int Fav_Car;
The switch case code is as follows 开关盒代码如下

switch( Fav_Car ) {
    case '1' :
        cout<< "That's cool";
        break;
    case '2' :
        cout<< "Even mine!";
        break;
    default :
        cout<< "Oh";
        break;
}

When I ran the program, I noticed that using int returns me the default case and using char works perfectly. 当我运行程序时,我注意到使用int会返回默认情况,而使用char则可以完美工作。 Why does it happen so? 为什么会这样呢?
And also, what's the difference if I use case '1' : and case "1" : 而且,如果我使用case '1' :case "1" :什么区别case "1" :

Your misunderstanding has nothing to do with the switch() construct, it's all about the single quotes '' : If you write 1 , you get an integer of the value 1, when you put it in single quotes '1' , you get the numeric value of the ASCII character for the digit 1 (this is a bit imprecise, see note below). 您的误解与switch()构造无关,仅与单引号'' :如果编写1 ,您将得到一个值为1的整数,将其放在单引号'1' ,您将得到数字1的ASCII字符的数字值(这有点不精确,请参见下面的注释)。 That ASCII character has the numeric value of 0x31 , or 49 in decimal. 该ASCII字符的数字值为0x31或十进制的49 Now imagine the difference between 现在想象一下之间的区别

switch( Fav_Car ) {
    case 1 :
        cout<< "That's cool";
        break;
    case 2 :
        cout<< "Even mine!";
        break;
    default :
        cout<< "Oh";
        break;
}

and

switch( Fav_Car ) {
    case 49 :
        cout<< "That's cool";
        break;
    case 50 :
        cout<< "Even mine!";
        break;
    default :
        cout<< "Oh";
        break;
}

The second one is equivalent to the version that you posted, and I think it's clear why it behaves very differently from the first version. 第二个与您发布的版本等效,我认为很清楚为什么它的行为与第一个版本大不相同。


Note: 注意:
While '1' yields an ASCII character value in most C++ implementations, that does not need to be the case. 尽管在大多数C ++实现中, '1'产生ASCII字符值,但情况并非必须如此。 The implementation is free to use some other character code, so that the value of '1' is actually implementation defined. 该实现可以自由使用其他字符代码,因此'1'值实际上是实现定义的。 It could be virtually anything except zero (because that's used for the terminating null-byte in strings). 它几乎可以是除零以外的任何内容(因为它用于终止字符串中的空字节)。 However, most implementations do use ASCII encoding, which is why I assumed ASCII in the text above. 但是,大多数实现都使用ASCII编码,这就是为什么我在上面的文本中假定使用ASCII。

What's the difference between char and int in a switch case? 在开关盒中,char和int有什么区别?

Using char or int are both ok in a switch statement. switch语句中使用charint都可以。 It depends on how you input your Fav_Car - as long as the input matches with a case , that case will be executed. 这取决于您如何输入Fav_Car只要输入与case匹配,便会执行该case

Note that char is also an integral type - it has a value in range [32, 127] (assume you want a printable char). 请注意, char也是整数类型-它的值在[32,127]范围内(假设您要使用可打印的char)。

what's the difference if I use case '1' : and case "1" 如果使用案例'1'和案例“ 1”有什么区别?

switch case only work with integral (ie int , char ). switch case仅适用于整数(即intchar )。 So: 所以:

case '1':   // ok.

case "1":   // wrong because "1" is a string - not integral type.

Why does it happen? 为什么会发生? Because in your switch case, you use a char, not int. 因为在切换的情况下,您使用的是char,而不是int。

What is the difference between them? 它们之间有什么区别?

1 //int
'1' // char 1
"1" // string 1

To use int 使用int

int a;
switch(a){
    case 1 :
}

Your problem is that '1' is not the same thing as 1 . 你的问题是, '1'是不一样的东西1

'1' means the printable '1' character literal, which on any computer using the ASCII character map is actually the integer 49. '1'表示可打印的 “ 1”字符文字,在使用ASCII字符映射表的任何计算机上,它实际上是整数49。

For a demonstration of the difference, try this: 为了说明差异,请尝试以下操作:

char a = 1;
char b = '1';
int x  = a;
int y =  b;
cout << "a as int: " << x << "\n";
cout << "b as int: " << y;

because '1' is a char and 1 is an integer. 因为“ 1”是一个字符,而1是一个整数。 If you want to make integers work , remove the single quotes. 如果要使整数有效,请删除单引号。 Using double quotes makes it a string. 使用双引号使它成为字符串。

Are you sure that INT is not working? 您确定INT无法正常工作吗?

The following code works well: 以下代码运行良好:

#include <iostream>

int main() {
    int fav_car = 2;
switch(fav_car) {
        case 1 :
            std::cout<< "That's cool";
        break;
        case 2 :
            std::cout<< "Even mine!";
        break;
        default :
            std::cout<< "Oh";
        break;
    }
}

case '1' - it is a symbol 情况“ 1”-它是一个符号

case "1" - it is a string constant 情况“ 1”-它是一个字符串常量

Characters in switch cases are eventually converted to ASCII equivalent decimal ie 转换情况下的字符最终转换为ASCII等效的十进制,即

char '1' - int 49 
char '2' - int 50

For example, if input is integer int 1 , switch case will switch to default because 1 doesn't satisfy any case. 例如,如果input为integer int 1 ,则切换大小写将切换为default因为1不满足任何情况。

1 != 49
1 != 50

However, in case input is character char '1' , output will be the first case as your desire. 但是,如果输入是字符 char '1' ,则输出将是您所希望的第一种情况。

In this case: none; 在这种情况下:无; there is no difference in the way your program will execute its switch statement. 程序执行switch语句的方式没有任何区别。 Both char and int are Integral Types: they represent integers. charint都是整数类型:它们表示整数。 char is typically unsigned and at least 8 bits (0-255) and int is signed and typically 32-bits (-2 billion to + 2 billion). char通常是无符号的,并且至少为8位(0-255),而int是有符号的,通常是32位(-20亿至+ 20亿)。

Note that char represents a single character, not a string; 请注意, char表示单个字符,而不是字符串。 and that you cannot use a string value in a switch statement in the way you can in C#, Java and Swift, as switch compiles-down to an in-memory hashtable for ultra-fast performance, that optimization cannot be done with string types currently. 并且您不能像在C#,Java和Swift中那样在switch语句中使用字符串值,因为switch编译为内存哈希表以实现超快性能,因此当前无法使用字符串类型进行优化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM