The not(!) typically means its opposite.
Since booleans have only two possibilities (true and false), !0 would be 1.
http://www.cplusplus.com/doc/boolean/
1 usually represents yes/true, 0 usually represents no/false.
If (a.compare(b) == !0) //If (compare's returned value) (is equal) (not false); Not false literally means true(1).
Using the reference from before:
http://www.cplusplus.com/reference/string/string/compare/
a.compare(b) will return a value less than 0 (<0) because its first character 'a', from string a, has a smaller integer value than 'f', from string b; in this case, you got -1.
Normally it would return 0 if a complete match was found, and a value greater than 0 if the first string was larger; aka 1.
http://www.asciitable.com/
'a' = 97
'f' = 102
So, if (-1) (is equal to) (not false(0) aka
true(1))
print out "not equal"
This would never happen, since boolean could only have a value of 0 or 1
This could be consider an undefined behavior, because we were expecting any value that was not 0 to be valid, but in this case, -1 was also not valid.
If your carious as to what the result of the comparison was you can try this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
std::string a = "abc";
std::string b = "fda";
cout<<"Result of comparison of string a and string b: "<<a.compare(b)<<endl;
cout<<"Result of comparison of string b and string a: "<<a.compare(b)<<endl;
if (a.compare(b) == !0) {
std::cout << "not equal";
}
cin.get();
return 0;
}
|
Edit: Posted at 3 AM, hopefully it's legible.