Output and Input

The string class overloads the << operator to display string objects. It returns a reference to the istream object so that output can be concatenated:

string claim("The string class has many features.");
cout << claim << endl;

The string class overloads the >> operator so that you can read input into a string:

string who;
cin >> who;

Input terminates on end-of-file, reading the maximum number of characters allowed in a string, or on reaching a whitespace character. (The definition of whitespace will depend on the character set and upon the type charT represents.)

There are two getline() functions. The first has this prototype:

 template<class charT, class traits, class Allocator> basic_istream<charT,traits>&getline(basic_istream<charT,traits>&is, ...

Get The Waite Group's C++ Primer Plus, Third 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.