The apmatrix class implements resizable two-dimensional arrays with safe subscripting.
It is similar to the apvector class with the following differences --
resize(..)
function take two arguments,
rows and cols, instead of one, sizelength( )
function is replaced by two functions,
numrows( )
and numcols( )
.
Here are examples of the four apmatrix constructors -
|
Here are some more examples of how to use apmatrices in a program -
#include "apmatrix.h" // include the header file int rows = table.numrows( ); // determine the current number of rows of the matrix int cols = table.numcols( ); // determine the current number of columns of the matrix table.resize(5,3); // set new dimensions of the matrix, truncating extra // elements and filling new elements with default values, // (zeros for int, double, char types) table[0][1] = 4.2; // store 4.2 in the first row, second column of the matrix // at runtime checks that subscripts are within legal rangeThese two prototype functions show how to always pass matrix objects by reference.
void fillMatrix(apmatrix&table); // Note that the & must be explicitly used when a matrix object is passed by reference. void displayMatrix(const apmatrix &table); // To avoid unnecessary copying, do not pass an object by value . // Pass by reference using the keyword const to ensure the object cannot be changed