Initializing complex objects from file input
Reading in individual integers, floats, and word strings is really easy, because the >>
operator of input stream objects is overloaded for all these types, and input streams conveniently drop all in-between whitespace for us.
But what if we have a more complex structure that we want to read from an input stream, and if we need to read strings that contain more than one word (as they would normally be chunked into single words because of the whitespace skipping)?
For any type, it is possible to provide another input stream operator>>
overload, and we are going to see how to do it.
How to do it...
In this section, we'll define a custom data structure and provide facilities to read such items from input streams as standard input:
- We need to include some headers first and for comfort, we declare that we use the
std
namespace by default:
#include <iostream> #include <iomanip> #include <string> #include...