Comparing string Class Strings

Life is a bit simpler if you use string class strings instead of C-style strings because the class design allows you to use relational operators to make the comparisons. This is possible because one can define class functions that “overload,” or redefine, operators. Chapter 12, “Classes and Dynamic Memory Allocation,” discusses how to incorporate this feature into class designs, but from a practical standpoint, all you need to know now is that you can use the relational operators with string class objects. Listing 5.12 revises Listing 5.11 to use a string object instead of an array of char.

Listing 5.12. compstr2.cpp

// compstr2.cpp -- comparing strings using arrays#include <iostream>#include <string>     // string ...

Get C++ Primer Plus now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.