string
sClass string
provides member functions for comparing string
s. Figure 21.2 demonstrates class string
’s comparison capabilities.
The program declares four string
s (lines 8–11) and outputs each (lines 13–14). Line 17 tests string1
against string4
for equality using the overloaded equality operator. If the condition is true
, "string1 == string4"
is output. If the condition is false
, the condition in line 20 is tested. All the string
class overloaded relational and equality operator functions return bool
values.
Line 28 uses string
member function compare
to compare string1
to string2
. Variable result
is assigned 0
if the string
s are equivalent, a positive number if string1
is lexicographically
greater than string2
or a negative number if string1
is lexicographically less than string2
. When we say that a string
is lexicographically less than another, we mean that the compare
member function uses the numerical values of the characters (see Appendix B, ASCII Character Set) in each string
to determine that the first string
is less than the second. A lexicon is a dictionary. Because a string
starting with 'T'
is considered lexicographically greater than a string starting with 'H'
, result
is assigned a value greater than 0
, as confirmed by the output.
Line 41 compares portions of string1
and string3
using an overloaded version of member function compare
. The first two arguments (2
and 5
) specify the starting subscript and length of the portion of string1
("sting"
) to compare with string3
. The third argument is the comparison string
. The last two arguments (0
and 5
) are the starting subscript and length of the portion of the comparison string
being compared (also "sting"
). The value assigned to result
is 0
for equality, a positive number if string1
is lexicographically greater than string3
or a negative number if string1
is lexicographically less than string3
. The two pieces being compared here are identical, so result
is assigned 0
.
Line 54 uses another overloaded version of function compare
to compare string4
and string2
. The first two arguments are the same—the starting subscript and length. The last argument is the comparison string
. The value returned is also the same—0
for equality, a positive number if string4
is lexicographically greater than string2
or a negative number if string4
is lexicographically less than string2
. Because the two pieces of string
s being compared here are identical, result
is assigned 0
.
Line 67 calls member function compare
to compare the first 3
characters in string2
to string4
. Because "Hel"
is less than "Hello"
, a value less than zero is returned.