简体   繁体   English

'a'=='b'。这是一个很好的方法吗?

[英]'a' == 'b'. It's a good way to do?

What happens if I compare two characters in this way: 如果我以这种方式比较两个字符会发生什么:

if ('a' == 'b')
    doSomething();

I'm really curious to know what the language (and the compiler) does when it finds a comparison like this. 我真的很想知道语言(和编译器)在找到这样的比较时会做什么。 And, of course, if it is a correct way to do something, or if I have to use something like strcmp() . 当然,如果这是一种正确的做法,或者我必须使用像strcmp()这样的东西。

EDIT Wait wait. 编辑等待等待。
Since someone haven't understood what I really mean, I decided to explain in another way. 既然有人不明白我的意思,我决定以另一种方式解释。

char x, y;
cout << "Put a character: ";
cin >> x;
cout << "Put another character: ";
cin >> y;

if (x == y)
    doSomething();

Of course, in the if brackets you can replace == with any other comparison operator. 当然,在if括号中你可以用任何其他比较运算符替换==
What really I want to know is: how the character are considered in C/C++? 我真正想知道的是:如何在C / C ++中考虑角色? When the compiler compares two characters, how does it know that 'a' is different than 'b'? 当编译器比较两个字符时,它如何知道'a'与'b'不同? It refers to the ASCII table? 它指的是ASCII表?

您可以通过比较运算符==绝对安全地比较基本类型

In C and C++, single character constants (and char variables) are integer values (in the mathematical sense, not in the sense of int values). 在C和C ++中,单个字符常量(和char变量)是整数值(在数学意义上,不是在int值的意义上)。 The compiler compares them as integers when you use == . 当您使用==时,编译器将它们作为整数进行比较。 You can also use the other integer comparison operators ( < , <= , etc.) You can also add and subtract them. 您还可以使用其他整数比较运算符( <<=等)。您也可以添加和减去它们。 (For instance, a common idiom to change a digit character into its numerical value is c - '0' .) (例如,将数字字符更改为其数字值的常用习惯用法是c - '0' 。)

For single chars , this form is correct. 对于单个chars ,此表单是正确的。 If both operands are known at compile time as in your example, then the condition can (and almost certainly will) be evaluated at compile time and not result in any code. 如果两个操作数在编译时都是已知的,那么条件可以(并且几乎肯定会)在编译时进行评估,而不会产生任何代码。

Note that a char ( 'a' ) is different from a single-character string ( "a" ). 请注意, char'a' )与单字符字符串( "a" )不同。 For the latter, comparison has a different meaning: it would compare the pointers rather than the characters. 对于后者,比较具有不同的含义:它将比较指针而不是字符。

Your processor would subtract both operands and if it's zero, zero condition bit is set, your values were the same. 您的处理器将减去两个操作数,如果它为零,则设置零条件位,您的值相同。

For example: on arm machines you have the nzcv (negative, zero, carry, overflow) bits which tell you what happened. 例如:在arm机器上你有nzcv(负,零,进位,溢出)位,告诉你发生了什么。

Nothing will happen as a doesn't equal b . 会发生什么比a不等于b

If you question is just about is that the correct way, then the answer is yes. 如果你问的是正确的方法,那么答案是肯定的。

First 'a' and 'b' are not strings, they are characters. 首先'a'和'b'不是字符串,它们是字符。 The nuance is important because of its implications. 由于其影响,细微差别很重要。

You can compare characters to characters just fine the same way you can compare integers to integers and floats with floats. 您可以将字符与字符进行比较,就像将整数与整数和浮点数与浮点数进行比较一样。 It's usually not done because the outcome will always be the same. 它通常没有完成,因为结果总是一样的。 ie 'a' == 'b' will always be false. 'a' == 'b'将永远是假的。

If you're comparing strings, however, you'll have to use something like strcmp() . 但是,如果您要比较字符串,则必须使用类似strcmp()

Compiler simply inserts an instruction for comparing two bytes for equality - a very efficient operation. 编译器只是插入一条指令来比较两个字节的相等性 - 一个非常有效的操作。 Of course in your case 'a'=='b' is equivalent to a constant false . 当然在你的情况下'a'=='b'相当于一个常数false

The compiler will compare the numeric ASCII codes. 编译器将比较数字ASCII代码。 So, 'a' is never equal to 'b'. 所以,'a'永远不会等于'b'。 But, 'a' < 'b' evaluates to true, since 'a' appears before 'b' in the ASCII table. 但是, 'a' < 'b'计算结果为true,因为'a'出现在ASCII表中的'b'之前。

Of course, you want to use variables like 当然,你想使用像变量

char myChr = 'a' ;
if( myChr == 'b' ) puts( "It's b" ) ;

Now you can start to think about "Yoda conditions" , where you would do 现在你可以开始考虑“尤达条件” ,你会去做什么

if( 'b' == myChr )  puts( "It's a b" ) ;

so that in case you accidently typed one equals sign in the 2nd example: 以防万一你在第二个例子中意外输入一个等号:

if( 'b' = myChr ) puts( "It's a b" ) ;

that would raise a compiler error 这会引发编译器错误

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

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