在 C++ 中比较 char 和 Int

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22883451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:15:23  来源:igfitidea点击:

Comparing char to Int in c++

c++typescastingcharint

提问by user3029001

in c++, is it okay to compare an int to a char because of implicit type casting? Or am I misunderstanding the concept?

在 C++ 中,由于隐式类型转换,可以将 int 与 char 进行比较吗?还是我误解了这个概念?

For example, can I do

例如,我可以做

int x = 68;
char y;
std::cin >> y;
//Assuming that the user inputs 'Z';
if(x < y) 
{
 cout << "Your input is larger than x";
}

Or do we need to first convert it to an int?

还是我们需要先将其转换为 int?

so

所以

 if(x < static_cast<int>(y)) 
{
 cout << "Your input is larger than x";
}

采纳答案by Basile Starynkevitch

Yes you can compare an intto some char, like you can compare an intto some short, but it might be considered bad style. I would code

是的,您可以将 anint与 some进行比较char,就像您可以将 anint与 some进行比较short,但它可能被认为是不好的风格。我会编码

if (x < (int)y) 

or like you did

或者像你一样

if (x < static_cast<int>(y))

which I find a bit too verbose for that case....

对于那种情况,我觉得这有点太冗长了....

BTW, if you intend to use bytes not as charconsider also the int8_ttype (etc...) from <cstdint>

顺便说一句,如果您打算使用字节而不是char考虑int8_t来自<cstdint>

Don't forget that on some systems, charare signedby default, on others they are unsigned(and you could explicit unsigned charvs signed char).

不要忘了,在某些系统上,charsigned在默认情况下,别人他们是unsigned(你能明确unsigned charVS signed char)。

回答by cmaster - reinstate monica

The problem with bothversions is that you cannot be sure about the value that results from negative/large values (the values that are negative if charis indeed a signed char). This is implementation defined, because the implementation defines whether charmeans signed charor unsigned char.

两个版本的问题在于您无法确定由负/大值(如果char确实是 a 则为负的值)产生的值signed char。这是实现定义的,因为实现定义了char手段signed char还是unsigned char

The only way to fix this problem is to cast to the appropriate signed/unsigned chartype first:

解决此问题的唯一方法是首先转换为适当的有符号/无符号字符类型:

if(x < (signed char)y)

or

或者

if(x < (unsigned char)y)

Omitting this cast will result in implementation defined behavior.

省略此强制转换将导致实现定义的行为。

Personally, I generally prefer use of uint8_tand int8_twhen using chars as numbers, precisely because of this issue.

就个人而言,我通常更喜欢使用uint8_tint8_t将字符用作数字,正是因为这个问题。



This still assumes that the value of the (un)signed charis within the range of possible intvalues on your platform. This may not be the case if sizeof(char) == sizeof(int) == 1(possible only if a charis 16 bit!), and you are comparing signed and unsigned values.

这仍然假设 的值在您平台上(un)signed char的可能int值范围内。如果sizeof(char) == sizeof(int) == 1(仅当 achar为 16 位时才有可能!),并且您正在比较有符号和无符号值,则情况可能并非如此。

To avoid this problem, ensure that you use either

为避免此问题,请确保您使用

signed x = ...;
if(x < (signed char)y)

or

或者

unsigned x = ...;
if(x < (unsigned char)y)

Your compiler will hopefully complain with warning about mixed signed comparison if you fail to do so.

如果您不这样做,您的编译器可能会警告有关混合签名比较的警告。

回答by Deduplicator

Your code will compile and work, for some definition of work.

您的代码将编译并工作,用于某些工作定义。

Still you might get unexpected results, because yis a char, which means its signedness is implementation defined. That combined with unknown size of int will lead to much joy.

你仍然可能会得到意想不到的结果,因为yis a char,这意味着它的签名是实现定义的。结合未知大小的 int 会带来很多乐趣。

Also, please write the char literals you want, don't look at the ASCII table yourself. Any reader (you in 5 minutes) will be thankful.

另外,请写你想要的char文字,不要自己看ASCII表。任何读者(5 分钟后的你)都会感激不尽。

Last point: Avoid gratuituous cast, they don't make anything better and may hide problems your compiler would normally warn about.

最后一点:避免无故强制转换,它们不会使任何事情变得更好,并且可能隐藏编译器通常会警告的问题。

回答by Ivaylo Strandjev

The code you suggest will compile, but I strongly recommend the static_castversion. Using static_castyou will help the reader understand what do you compare to an integer.

您建议的代码将编译,但我强烈推荐该static_cast版本。使用static_cast你将帮助读者理解你与整数的比较。