Reading numbers from strings using streams

The cin object, an instance of the istream class (in the <istream> library), can input characters from the console and convert them to the numeric form you specify. The ifstream class (in the <ifstream> library) will also allow you to input characters from a file and convert them to numeric form. As with outputting streams, you can use the stream classes with a string buffer so that you can convert from a string object to a numeric value.

The basic_istringstream class (in the <sstream> library) is derived from the basic_istream class, so you can create stream objects and extract items (numbers and strings) from these objects. The class provides this stream interface on a string object (the typedef ...

Get Modern C++: Efficient and Scalable Application Development 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.