Reputation: 31
How come when comparing strings...
string e = "11"
string f = "102"
string s = "8"
e > s - this statement is false
f > s - and this is also false
How come those statements are false? And what are the rules when comparing two strings to each other?
Upvotes: 0
Views: 722
Reputation: 48696
Using relational operators on strings in C/C++ will simply compare the memory addresses of the strings. Obviously "11"
and "8"
are occupying 2 different areas of memory, so it could be false, unless the address where "11"
is stored happens to be stored in an address greater than "8"
, but it's random.
Keep in mind that you can use string::compare
, however, it's comparing the ASCII code's of the strings. Since "1"
(ASCII Code 49) is less than "8"
(ASCII Code 56), it will still be false. You need to use stoi
to convert the string to an integer, then compare the integers.
Upvotes: 2