Parallel vector Tedium No More!

vector's were great for collecting together multiple values of the same data type -- multiple double's for length measurements, multiple string's for names, etc. But, what if the data about something is more complicated and requires more than one data type? (Or, even, more than one value of the same type?)

For instance, a time of day actually consists of at least two short integers: an hour and a minute. To store this with vector's, we'd need to use a pair of parallel vector's -- one for the hours and another for the minutes:

    vector<short> hours, minutes;
    // now I have to make sure every hour has a matching minute:
    short hr, min;
    char t;
    cin >> hr >> t >> min;
    hours.push_back(hr);
    minutes.push_back(min);  // gotta keep 'em parallel!

With a struct'ure, we can combine both short integers into a single memory location:

    struct TimeOfDay
    {
	short hour, minute;
    };

Now we can simply have a single vector each element of which holds both parts of our time:

    vector<TimeOfDay> times;
    // every addition contains all the data I need -- no worries
    // about messing up parallelism!
    TimeOfDay tim;
    char t;
    cin >> tim.hour >> t >> tim.minute;
    times.push_back(tim);

If I wanted to print the pth time with the parallel vector's, I'd do this:

    vector<short>::size_type p;
    // ... fill in p somehow ...
    cout << hours[p] << ':' << minutes[p];

But, with the struct'ure in play, I can simply do:

    vector<TimeOfDay>::size_type p;
    // ... fill in p somehow ...
    cout << tim[p].hour << ':'
         << tim[p].minute;

That's a bit better.

But, let's coalesce this into a single idea. If I have a set of parallel vector's, I can make a struct'ure with a single field/member for each of the vector's. Next I replace all the parallel vector's with a single vector of the struct'ure type as its base-type.


Let's try another example: labeled points. To store a labeled point as parallel vector's, I'd need three: a string base and two double based. First to make the struct'ure:

    struct LabeledPoint
    {
	string label;
	double x, y;
    };

Notice that we've created a single field of this struct'ure for each of the original parallel vector's. Now for the single vector:

    vector<LabeledPoint> points;

Each item placed into the vector 'points' will have a label and both x and y coordinates:

   points
    +--------------------+--------------------+-----
    | label | "origin" | | label | "pond" |   | ...
    | x | 0 |  y | 0 |   | x | 12 |  y | -3 | | ...
    +--------------------+--------------------+-----
      0                    1

Here [size_type-style] position 0 is (0,0) and is labeled "origin". Likewise [size_type-style] position 1 is (12,-3) and is labeled "pond". So, each position in the vector that exists (position < points.size()) will have in it a string and two double's.

Filling in the vector

...very similar to advanced class: class as vector base type...

Note...

A Student class with parallel vectors is seen here. It's conversion to struct'ure form is shown here. The second one also contains some advanced class goodies. Check them out!

More to come..?