if I should use vector-of-vectors instad of pointer-to-pointer, if my purpose is to do matrix arithmetic in C++ |
"pointer-to-pointer" is something you'd use when learning C, and not because it makes a good matrix (it doesn't!), but because it teaches a beginner C programmer to juggle dynamic memory allocation of two kinds of arrays with nested lifetimes.
You can do vector of vectors in C++, it has the same runtime characteristics, makes a whole lot more sense for a C++ program, but doesn't make a good matrix either - as with the "pointer to pointer" approach, the individual rows of your matrix are allocated separately all over the RAM.
If you want to go one step closer to a real matrix, try building a Matrix class, which holds a 1D storage container internally -- std::valarray or std::vector -- and converts between (row, column) notation and the 1D index in getters/setters. See C++ FAQ Lite for some useful suggestions:
http://www.parashift.com/c++-faq/matrix-subscript-op.html
If you want to
use matrix arithmetic in C++, use a matrix library: boost.ublas, Eigen, etc - they use expression templates for matrix arithmetic, which aren't easy to write yourself.