I had a past paper question involving the STL sort algorithm. And it was sorting a vector, i am aware that the sort algorithm sorts into ascending order using the "<" operator. However, I was wondering how two strings are compared? How is a value for a string found? (So i can work out what string is smaller than another in an exam.)
Asked
Active
Viewed 135 times
-3
-
http://en.cppreference.com/w/cpp/string/basic_string/operator_cmp – Jack May 06 '15 at 17:08
1 Answers
1
Strings are ordered lexicographically. Each character in the first string is compared to the equivalent character in the second string until there are two characters that don't match one another or until one string ends. For example the following statements are true:
"aaab" < "aaac" // because 'b'<'c'
"aaa" < "aaab" // by convention shorter string are smaller than larger ones
"aaa0" < "aaab" // because '0'=48 and 'b'=98
"aaaB" < "aaab" // because 'B'=66 and 'b'=98
The character comparison is performed using the numeric values of the corresponding characters (using ASCII for instance).

Benjy Kessler
- 7,356
- 6
- 41
- 69