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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.