liveBook · Manning

Extraction operator

As explained in 100 C++ Mistakes and How to Avoid Them

The extraction operator is the overloaded C++ operator>> used to read and convert formatted data from an input stream.

The extraction operator (>>) reads formatted character data from an input stream and converts it according to the destination variable's type. Unlike scanf format strings, it avoids separate conversion specifiers and allows each input operation to be checked independently. section 6.1

Read and check values incrementally

An istringstream can read a double, integer, string, and another integer in sequence: str >> pi; str >> cats; str >> buffer; str >> mice;. Check str.fail() after each operation to identify the failing value. Consistent input converts correctly; inconsistent input sets the stream's failure state. section 6.1

How 100 C++ Mistakes and How to Avoid Them covers it

How deep it goes
Explains typed extraction, inconsistent-input failure, per-operation checks, and optional base-eight input, but not operator overloading internals.
Examples
Demonstrates extracting pi, cat counts, text, and mice from an istringstream, then selecting octal input with setbase.
What it assumes
Assumes basic C++ variables and streams while contrasting them with scanf and printf format-specifier usage.
What you can do after
Apply it via C++ streams to read typed values and diagnose which extraction fails.
How it connects
Connects input streams, insertion operators, istringstream, formatted conversion, and stream failure states.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 C++ Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.