The following code prints
1
0
And I've been wondering why the values are different if the comparisons are using the same string… I've been struggling with this for a while and cannot figure out why they return different boolean values.
int main()
{
string stringArray[] = { "banana","Banana","zebra","apple","Apple","Zebra","cayote" };
cout << (stringArray[1] < stringArray[0]) << endl;
cout << ("Banana" < "banana") << endl;
return 0;
}
stringArray[n]
is an std::string
, but "Banana"
is a string literal (an array of char
s).
When you do "Banana" < "banana"
, both string literals are implicitly converted to char
pointers pointing to the char
array. <
then compares those memory addresses.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With