Skip to main content

10.3) Vector Based Equivalent


typedef vector<double> row;
typedef vector<row> matrix2d;
...
matrix2d a(4,row(5));      // 4x5 matrix of zeroes
matrix2d b(5, row(3,1));   // 5x3 matrix or ones
a[0][0] = 3.14159;         // assigns to top-left element of a
cout << b[4][2] << endl;   // prints bottom-right element of b
  • A 2D matrix can be implemented as a 'vector of vectors'
    • Each row is a vector of numeric values
    • Matrix is a vector of these rows
  • Definitions are simplified by the use of typedef
  • The syntax for matrix initialization is a little odd; it would probably be better to use a dedicated matrix class designed for such representations - e.g., one from the Boost libraries