7.13. strings are comparableΒΆ

All the comparison operators that work on ints and doubles also work on strings.

Take a look at the active code below, which checks to see if word is equal to "banana".

Strings are comparable
1#include <iostream>
2#include <string>
3
4int main() {
5    std::string word = "banana";
6    if (word == "banana") {
7        std::cout << "Yes, we have no bananas!\n";
8    }
9}

The same rules and limitations that apply to the + operator apply to the relational comparison operators. In order to work correctly, at least one of the operands must be a std::string. C strings by themselves are not comparable. These comparisons work because one of the operands is a std::string.

The other comparison operations are useful for putting words in alphabetical order.

The active code below uses comparison operators to determine the ordering of word relative to "banana".

Strings are comparable
 1#include <iostream>
 2#include <string>
 3
 4int main() {
 5
 6  std::string word = "Zebra";
 7  using std::cout;
 8
 9  if (word < "banana") {
10    cout << "Your word, " << word << ", comes before banana.\n";
11  } else if (word > "banana") {
12    cout << "Your word, " << word << ", comes after banana.\n";
13  } else {
14    cout << "Yes, we have no bananas!\n";
15  }
16}

You should be aware, though, that the string class does not handle upper and lower case letters the same way that people do. All the upper case letters come before all the lower case letters. As a result,

Your word, Zebra, comes before banana.

A common way to address this problem is to convert strings to a standard format, like all lower-case, before performing the comparison. The next sections explains how. I will not address the more difficult problem, which is making the program realize that zebras are not fruit.

Sometimes we want to print the result of a comparison operation. When we use cout to print the value, we don't get what we expect.

Print a simple boolean value resulting from a string comparison.

String comparisons print 0 and 1 by default
1#include <iostream>
2#include <string>
3
4int main() {
5  std::string word = "Zebra";
6  bool compare = word < "banana";
7  std::cout << "Comparison: " << compare;
8}

The bool value is actually getting converted to an integer when processed by the cout class.

In order to get the results we expect, we need to use the input / output manipulation library: iomanip.

The I/O manipluator boolalpha can display the boolean values as the words true or false.

iomanip can change what iostream displays
1#include <iomanip>
2#include <iostream>
3#include <string>
4
5int main() {
6  std::string word = "Zebra";
7  bool compare = word < "banana";
8  std::cout << std::boolalpha << "Comparison: " << compare;
9}

Q1

What would the result of the following comparison be? Where 1 means true and 0 means false.

"Dog" < "Doghouse";

Q2

What would the result of the following comparison be? Where 1 means true and 0 means false.

"dog" < "Dog";

Q3

What would the result of the following comparison be? Where 1 means true and 0 means false.

"dog" < "Doghouse";

Q4

What would the result of the following comparison be? Where 1 means true and 0 means false.

"bread" < "bread";