cout << boolalpha ("1" < "0") << endl;
I was compiling this recently as a spin off from some course work I was doing. Why does this produce true when I execute it?
cout << boolalpha (string("1") < string("0")) << endl;
does the comparison as expected.
It’s comparing
const char*, the result of"1"and"0"is undefined by the standard, whereas the comparison of 2std::strings is defined and your output in that case is expected.Quick case in point:
and
I’m specifying “on my platform” because there’s no standard rule (but it can be a compiler rule) to where the pointers are created or in which order they are created.
In my case, the addresses are assigned in reverse order of declaration.
I’m willing to bet that if you run:
and
you’d get the same output (although it’s not a rule). Note that you should run them in different instances of the program. If you run them in the same instance, you might get different results, as string literals reside in a single place in memory.