"Charulatha Kalluri" <(E-Mail Removed)> wrote in message

news:csfdc0$mti$(E-Mail Removed)...

> Hi, I'm implementing a Matrix class, as part of a project. This is the

> interface I've designed:

>

> class Matrix( )

> {

> private:

> vector< vector<int> > m_Data;

> pair<int, int> m_size;

> public:

> Matrix(unsigned int n);

> Matrix(unsigned int m, unsigned int n);

> ~Matrix();

> ....................................

> }

>

> I have a couple of questions:

> 1. Is there a better way to model the data structure other than a vector
of

> vectors? I realize I can write up the Matrix class from scratch, but I
want

> to use STL containers.
Yes, in fact a vector of vectors is a very bad way to implement a Matrix (at

least a mathematical one), because there is nothing that says the internal

vectors all have to be the same length.

The way to do it is to use a vector<T>, or perhaps a valarray<T> to provide

the internal representation of your data. You then have to build various

ways to view the data, keeping in mind the appropriate striding for the

matrix. There are tons of references about how to do this on the web .. try

a google search. A couple that I found useful in the past are:

Object oriented numerics:

http://www.oonumerics.org/oon/
and the POOMA library:

http://acts.nersc.gov/pooma/
Also, in chapter 22 of TC++PL 3rd ed., Stroustrup shows an example of how to

implement a Matrix using a std::valarray, along with some associated STL

helper classes for building different kinds of views. For me, the

non-aliasing restrictions placed on std::valarray were too strict, and I

ended up rolling my own using std::vector as an exercise, but the general

approach and examples he laid out were certainly useful.

HTH,

Dave Moore