15.1. From files to containers¶
A file stores characters; a program gives those characters meaning. In this chapter we read and write text files, parse records, and choose a container that fits the data. The examples use the C++20 standard library.
Our running example records distances between cities. A record contains two city names and a nonnegative distance. City names can contain spaces, so the file surrounds each name with double quotes:
"San Diego" "Los Angeles" 120
"Los Angeles" "San Francisco" 380
These are illustrative values, not a source for travel planning. We want to look up a known distance, list the cities without duplicates, and distinguish an unknown route from a route whose distance is zero.
The standard library provides containers for different jobs:
Container |
Organization |
A useful example |
|---|---|---|
|
A fixed number of elements in sequence |
The four suit names or seven daily readings |
|
A sequence that can grow |
Records read from a file of unknown length |
|
Unique keys in sorted order |
City names without duplicates |
|
Unique keys with associated values, sorted by key |
A city's neighbors and their distances |
We can combine these containers. A rectangular table can be an array of arrays when its dimensions are fixed, or a vector of vectors when dimensions are chosen at run time. A map of maps can represent only the routes we know. There is no need to implement another container just to store this data.
Why aren't we filling in every value in our table, who are we leaving blank space above the diagonal of 0's?
Based on how it is used to create the above table, what do you think a matrix is?