19.3. Comparing strings

Class string provides member functions for comparing strings. Figure 19.2 demonstrates class string’s comparison capabilities.


 1   // Fig. 19.2: Fig19_02.cpp
 2   // Comparing strings.
 3   #include <iostream>
 4   #include <string>
 5   using namespace std;
 6
 7   int main()
 8   {
 9      string string1( "Testing the comparison functions." );
10      string string2( "Hello" );
11      string string3( "stinger" );
12      string string4( string2 ); // "Hello"
13
14      cout << "string1: " << string1 << " string2: " << string2
15         << " string3: " << string3 << " string4: " << string4 << " ";
16
17      // comparing string1 and string4
18      if ( string1 == string4 )
19         cout << "string1 == string4 ";
20      else if ( string1 > string4 )
21            cout << "string1 > string4 ";
22      else // string1 < string4
23         cout << "string1 < string4 ";
24
25      // comparing string1 and string2
26      int result = string1.compare( string2 );
27
28      if ( result == 0 )
29         cout << "string1.compare( string2 ) == 0 ";
30      else if ( result > 0 )
31         cout << "string1.compare( string2 ) > 0 ";
32      else // result < 0
33         cout << "string1.compare( string2 ) < 0 ";
34
35      // comparing string1 (elements 2-5) and string3 (elements 0-5)
36      result = string1.compare( 2, 5, string3, 0, 5 );
37
38      if ( result == 0 )
39         cout << "string1.compare( 2, 5, string3, 0, 5 ) == 0 ";
40      else if ( result > 0 )
41         cout << "string1.compare( 2, 5, string3, 0, 5 ) > 0 ";
42      else // result < 0
43         cout << "string1.compare( 2, 5, string3, 0, 5 ) < 0 ";
44
45      // comparing string2 and string4
46      result = string4.compare( 0, string2.size(), string2 );
47
48      if ( result == 0 )
49         cout << "string4.compare( 0, string2.size(), "
50            << "string2 ) == 0" << endl;
51      else if ( result > 0 )
52         cout << "string4.compare( 0, string2.size(), "
53            << "string2 ) > 0" << endl;
54      else // result < 0
55         cout << "string4.compare( 0, string2.size(), "
56               << "string2 ) < 0" << endl;
57
58      // comparing string2 and string4
59      result = string2.compare( 0, 3, string4 );
60
61      if ( result == 0 )
62         cout << "string2.compare( 0, 3, string4 ) == 0" << endl;
63      else if ( result > 0 )
64         cout << "string2.compare( 0, 3, string4 ) > 0" << endl;
65      else // result < 0
66         cout << "string2.compare( 0, 3, string4 ) < 0" << endl;
67   } // end main


string1: Testing the comparison functions.
string2: Hello
string3: stinger
string4: Hello

string1 > string4
string1.compare( string2 ) > 0
string1.compare( 2, 5, string3, 0, 5 ) == 0
string4.compare( 0, string2.size(), string2 ) == 0
string2.compare( 0, 3, string4 ) < 0


Fig. 19.2. Comparing strings.

The program declares four strings (lines 9–12) and outputs each (lines 14–15). Line 18 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 26 uses string member function compare to compare string1 to string2. Variable result is assigned 0 if the strings 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 method 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. 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. A lexicon is a dictionary.

Line 36 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 46 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 strings being compared here are identical, result is assigned 0.

Line 59 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.56.45