Note: comando is a string
Code: Select all
if (comando == "quit") ...
Code: Select all
if (!comando.compare("quit")) ...
Moderator: Ras
Code: Select all
if (comando == "quit") ...
Code: Select all
if (!comando.compare("quit")) ...
I'll assume your code is C#.P. Villanueva wrote:Please could anyone explain me the difference between these two codes?
Note: comando is a string
Code: Select all
if (comando == "quit") ...
ThanksCode: Select all
if (!comando.compare("quit")) ...
I agree that the first example is much more clear. But it is not necessarily faster. Maybe both are equally fast, if they are both based on the "strcmp" algorithm which in fact does the same as you described for "compare". So if == is implemented as strcmp(x, y) == 0 then both are in fact nearly identical.mathmoi wrote:I'll assume your code is C#.P. Villanueva wrote:Please could anyone explain me the difference between these two codes?
Note: comando is a string
Code: Select all
if (comando == "quit") ...
ThanksCode: Select all
if (!comando.compare("quit")) ...
The first example compare two string and return true if they are the same.
The second example compare tow string and return a numerical value indicating if the first is greater or they are the same or the second is greater (http://msdn.microsoft.com/en-us/library ... S.71).aspx). Then the resulting value is transformed into a boolean and inversed.
In practice both example are equivalent, however, the second will be slower because it imply more work to get to the same result. Moreover, I personally find the second example less clear.
While I don't know how this works in C#, in C and C++ it is different: the resulting value, a signed integer, is converted to an integer from {0, 1} by applying the ! operator (nonzero into 0, zero into 1), and becomes a boolean only by being used within an if-expression.Then the resulting value is transformed into a boolean and inversed.
I am assuming C/C++, not sure if that is correct or not. If so, then...P. Villanueva wrote:Please could anyone explain me the difference between these two codes?
Note: comando is a string
Code: Select all
if (comando == "quit") ...
ThanksCode: Select all
if (!comando.compare("quit")) ...
It also isn't clear the first is correct. Could also be C/C++ code as well.Sven Schüle wrote:I agree that the first example is much more clear. But it is not necessarily faster. Maybe both are equally fast, if they are both based on the "strcmp" algorithm which in fact does the same as you described for "compare". So if == is implemented as strcmp(x, y) == 0 then both are in fact nearly identical.mathmoi wrote:I'll assume your code is C#.P. Villanueva wrote:Please could anyone explain me the difference between these two codes?
Note: comando is a string
Code: Select all
if (comando == "quit") ...
ThanksCode: Select all
if (!comando.compare("quit")) ...
The first example compare two string and return true if they are the same.
The second example compare tow string and return a numerical value indicating if the first is greater or they are the same or the second is greater (http://msdn.microsoft.com/en-us/library ... S.71).aspx). Then the resulting value is transformed into a boolean and inversed.
In practice both example are equivalent, however, the second will be slower because it imply more work to get to the same result. Moreover, I personally find the second example less clear.
One more nitpicking may be allowed (maybe I am wrong here w.r.t. C#):While I don't know how this works in C#, in C and C++ it is different: the resulting value, a signed integer, is converted to an integer from {0, 1} by applying the ! operator (nonzero into 0, zero into 1), and becomes a boolean only by being used within an if-expression.Then the resulting value is transformed into a boolean and inversed.
The result is of course the same ...
Sven
I admit I don't know how it's implemented, however it seems it would be a little bit more faster to simply check for equality than to do what strcmp do (Check for equality, then check . So I assume that's what the compiler does. But that's just an assumption.Sven Schüle wrote: I agree that the first example is much more clear. But it is not necessarily faster. Maybe both are equally fast, if they are both based on the "strcmp" algorithm which in fact does the same as you described for "compare". So if == is implemented as strcmp(x, y) == 0 then both are in fact nearly identical.
But the binary I built using "==" operator worked. Do different compilers have different idea about what "==" operator is?"commando" is a pointer to an array of characters. "quit" is _also_ a pointer to an array of characters. You are asking "are the addresses of these two strings the same?" and the answer will always be "no."
No, but different languages do. In C++ you have a std::string class with both a compare member and an operator== defined for it.P. Villanueva wrote:Thanks for the replies.
Its C++ code.
The story is that I used the "==" operator in KMT Chess and it worked fine (I use old Dev-C++ IDE with mingw32 as compiler).
But when Dann Corbit built the 64bit binary, he moved it to ".compare()". I didn´t understand why he did such think.
But the binary I built using "==" operator worked. Do different compilers have different idea about what "==" operator is?"commando" is a pointer to an array of characters. "quit" is _also_ a pointer to an array of characters. You are asking "are the addresses of these two strings the same?" and the answer will always be "no."
Code: Select all
command.Equals("quit") //works
command.CompareTo("quit")==0 //works
command == "quit" //works
!command.CompareTo("quit") //fails, using ! on int
!((bool)command.CompareTo("quit")) //fails, casting int to bool
//You can of course string together methods
if (command.Trim().ToLower().Equals("quit"))
...