How does the comparison operator for strings in C ++ work if strings are numbers?

Please tell me how less than the operator works for strings, if strings are numbers and also have the same number of digits. How exactly does this operator work?

For example, for the comparisons below -

cout<<("3" > "5")<<endl;
cout<<("31" > "25")<<endl;
cout<<("35" > "35")<<endl;
cout<<("38" > "85")<<endl;
cout<<("53" > "55")<<endl;
cout<<("36" > "35")<<endl;
cout<<("53" > "54")<<endl;

The output I got from CodeBlocks is

0
0
0
0
0
0
0
+6
source share
2 answers

Your code behavior is undefined.

The literals const char[]that you pointed out in terms of const char*pointers for comparison.

And the behavior of comparison operators in pointers is determined only if the pointers are part of the same array; which you don’t have.

s, . "3"s, ++ 14 std::string .

+12

"3" "", a const char s. const char const char*. operator<.

- . undefined , ( , ).


C, std::strcmp. std::string std::string::operator<.

+8

All Articles