More on string Class I/O

As you’ve seen, you can use cin with the >> operator to read a string object and cout with the << operator to display a string object using the same syntax you use with a C-style string. But reading a line at a time instead of a word at time uses a different syntax. Listing 4.10 shows this difference.

Listing 4.10. strtype4.cpp

// strtype4.cpp -- line input#include <iostream>#include <string>               // make string class available#include <cstring>              // C-style string libraryint main(){    using namespace std;    char charr[20];    string str;    cout << "Length of string in charr before input: "         << strlen(charr) << endl;    cout << "Length of string in str before input: "         << str.size() ...

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.