Section 14.8 Extra Dimensions
There is no reason that we have to limit ourselves to two dimensions. By nesting vectors we can create an arbitrary number of dimensions. For instance, imagine we wanted to represent ocean temperatures at various depths in different locations. We might define something like this:
vector<vector<vector<double>>> oceanTemperatures;
When representing 2 dimensions, rows and columns is a natural way to picture the data, even if what we are storing is really a list of lists.
With the structure shown for
oceanTemperatures
, what we really have is a list, where each item is a list. And each item of those lists is another list. But we can also visualize the data as something like rows, columns, and layers.
But the exact order of the dimensions is up to us. In the case of ocean temperatures, we might choose to have depth (or layer) be the first dimension. If the structure below is named
oceanTemps
, then oceanTemps.at(0)
would refer to the top layer. oceanTemps.at(0).at(0)
would refer to the uppermost latitude (row) in the top layer. And oceanTemps.at(0).at(0).at(2)
would refer to the rightmost (largest latitude) location at that latitude.
Traversal of a extra-dimensional structure follows the same patterns as for simpler structures. We just need one loop per dimension. For example, if we wanted to print out the ocean temperatures, we could do something like this:
You have attempted of activities on this page.