Use the string Class

The traditional C-style string suffers from not being a real type. You can store a string in a character array, you can initialize a character array to a string. But you can't use the assignment operator to assign a string to a character array; instead, you must remember to use strcpy() or strncpy(). You can't use the relational operators to compare C-style strings; instead, you must remember to use strcmp(). (And if you forget and use, say, the > operator, you don't get a syntax error; instead, the program compares string addresses instead of string contents.)

The string class (Chapter 16, “The string Class and the Standard Template Library,” and Appendix F, “The string Template Class”), on the other hand, lets you use objects ...

Get C++ Primer Plus, Fourth Edition 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.