NOTE:

These are a little sketchy...I'll fill in details 'soon'.

More Complicated Data

vectors are useful for storing simple data. But often we have data that is more complicated: points, times, dates, etc. For these situations, we used to use multiple variables. But if we wanted to have more than one or two times, this could get troublesome. In steps the parallel vector!

By placing two or more vectors side-by-side, we have a set of parallel vectors. The two (or more) vectors must have the same logical size (current number of elements). Then we use a single 'index' variable to access elements in both (all) of them.

    +-----+         +-----+         +-----+                 +-----+
    |     |         |     |         |     |                 |     |
    +-----+         +-----+         +-----+                 +-----+
    |     |         |     |         |     |                 |     |
    +-----+         +-----+         +-----+                 +-----+
    |     | <--i--> |     | <--i--> |     | <-- ...i... --> |     |
    +-----+         +-----+         +-----+                 +-----+
    |     |         |     |         |     |                 |     |
    +-----+         +-----+         +-----+                 +-----+
    |     |         |     |         |     |                 |     |
    +-----+         +-----+         +-----+                 +-----+
    |     |         |     |         |     |                 |     |
    +-----+         +-----+         +-----+                 +-----+
    |     |         |     |         |     |                 |     |
    +-----+         +-----+         +-----+                 +-----+

For instance, given the situation above where we want to collect many times-of-day together, we could have something like this:

  h
  o +------+------+------+------+------+------+------------+
  u |  12  |  13  |  15  |   9  |   5  |  10  |    ....    |
  r +------+------+------+------+------+------+------------+
  s

  m +------+------+------+------+------+------+------------+
  i |  45  |   2  |  18  |  27  |  42  |  53  |    ....    |
  n +------+------+------+------+------+------+------------+

By using a single index variable to access both the hours and min vectors, we can find out that the times stored here are 12:45, 13:02, 15:18, 9:27, etc. To print this information on screen, we could do something like this:

    vector<short> hours, min;
    vector<short>::size_type i;

    // fill in hours and min to resemble above or with whatever we need...

    for (i = 0; i != hours.size(); i++)
    {
        cout << hours[i] << ':' << min[i] << endl;
    }

So we're essentially keeping the two subscript operations synchronized with a common size_type'd integral counter.

Types Schmypes

The parallel vectors don't even need to have the same base types! If you were collecting information about the students in the class and you wanted to know their heights, the number of pencils they commonly carry, and their gender, you could use:

    vector<double> heights;
    vector<short> pencils;
    vector<char> genders;

to store this information. Note how they all have the same number of elements, but each vector has elements of a different base type.

And There's More!

You can also use the concept of parallel vectors with multiple dimensions. But we'll wait until we get there to discuss that. *smile*

A Better Way...

However, we'll often use a structure or class type as a base-type for a single vector rather than use parallel vectors. It simplifies the passing of arguments, etc. greatly!