Initialize Complex Structures From File Input

Learn to initialize complex structures from a file input.

We'll cover the following

One strength of the input stream is its ability to parse different types of data from a text file and convert them to their corresponding fundamental types. Here's a simple technique for importing data into a container of structures using an input stream.

How to do it

In this recipe, we'll take a data file and import its disparate fields into a vector of struct objects. The data file represents cities with their populations and map coordinates:

  • This is input.txt, the data file we'll read:

Get hands-on with 1200+ tech skills courses.