Skip to main content

Section 14.7 Functions for Multidimensional Vectors

When writing functions with multidimensional vectors, we should follow the same guideline as writing functions for normal vectors. Primarily, we should use pass by reference to avoid making needless copies of our data. If we don’t intend on modifying the data, we should use pass by const reference.
If we are writing multiple functions that all work with the same multidimensional data type, a typedef make make our code easier to read. For example, say we are working with two dimensional vectors of integers and want to write a function doubleAll that takes a table of data as a parameter, and returns a new table where all the values are doubled. We can either use the prototype on line 1, or we can do the typedef on line 3 and then write the prototype shown on line 4.
vector<vector<int>> doubleAll(const vector<vector<int>>& table);

typedef vector<vector<int>> IntTable;
IntTable doubleAll(const IntTable& table);
In this sample, we use two functions to work with a multidimensional collection of numbers:
Listing 14.7.1.
Note that these functions calculate the colCount for each row. Previous samples calculated one colCount based on the size of the first row. Calculating each row’s length independently means these functions will work correctly on β€œjagged” structures. Rectangular data has the same number of columns in every row. In non-rectangular or jagged data, each row can have a different length.
// Jagged data
1 2
3 4 5
6

// Rectangular data
1 2 3
4 5 6
If we knew the data was rectangular, we could calculate the column count once, based on the first row, instead of calculating it for each row. Something like:
size_t rowCount = table.size();
size_t colCount = table.at(0).size();
for (size_t row = 0; row < rowCount; ++row) {
    for (size_t col = 0; col < colCount; ++col) {
        ...

Checkpoint 14.7.1.

Which prototype makes the most sense for a function that will take in a multidimensional vector of strings and capitalize all the strings by modifying the table that was passed in?
  • void capitalizeAll(vector<vector<string>>& table)
  • vector<vector<string>> capitalizeAll(vector<vector<string>>& table)
  • This function prototype indicates that a new table will be returned. But we just want to modify the table that was passed in.
  • vector<vector<string>> capitalizeAll(vector<vector<string>> table)
  • This function prototype indicates that a new table will be returned. But we just want to modify the table that was passed in.
  • void capitalizeAll(const vector<vector<string>>& table)
  • This function prototype uses a const reference parameter. This means the function will not be able to modify the table that was passed in.
  • void capitalizeAll(vector<vector<string>> table)
  • This function prototype uses a value parameter. This means the function will modify a copy of the table, not the original table that was passed in.

Checkpoint 14.7.2.

Which prototype makes the most sense for a function that will take in a multidimensional vector of doubles called table and return a new multidimensional vector of doubles that has the square of all the values in table?
  • vector<vector<double>> squareAll(const vector<vector<double>>& table)
  • vector<vector<double>> squareAll(vector<vector<double>>& table)
  • This function prototype uses a non-const reference parameter. This means the function could modify the table that was passed in. But we just want to read the data and return a new table.
  • void squareAll(vector<vector<double>>& table)
  • This function prototype uses a non-const reference parameter. This means the function could modify the table that was passed in. But we just want to read the data and return a new table.

Checkpoint 14.7.3.

Which prototype makes the most sense for a function that will take in a multidimensional vector of integers called numbers and return a vector containing the sum of each row in the table?
  • vector<int> sumRows(const vector<vector<int>>& numbers)
  • vector<vector<int>> sumRows(vector<vector<int>>& numbers)
  • This function prototype indicates that a new multidimensional vector will be returned. But we just want to return a one-dimensional vector containing the sums of each row.
  • void sumRows(vector<vector<int>>& numbers)
  • This function prototype indicates that no value will be returned. But we want to return a one-dimensional vector containing the sums of each row.
  • vector<int> sumRows(vector<vector<int>>& numbers)
  • This function prototype uses a non-const reference parameter. This means the function could modify the table that was passed in. But we just want to read the data and return a new vector.
You have attempted of activities on this page.