Mail Archives: djgpp/1998/01/21/14:32:12
Thank you, groups, for your suggestions. My problem was confusing two different
methods of construction. I did not consider using a single vector with an
accessor method based on (m, n). My goal was a drop-in replacement for code
that operates on conventional C 2d arrays. Hence, the accessor method using
[m][n] was essential. Although I wrote a multiplication method to test my
accessor methods, I do not expect to use this class for matrix math. I am using
the code to implement an adjacency matrix, which I am using to solve a digital
logic problem. Since my adjacency matrix code operates on conventional C
arrays, preserving accessor semantics was extremely important to me. Your
milage may vary. Here is the class interface:
#include <vector>
template <class T>
class matrix {
private:
// Things that I want to avoid, generally speaking
public:
matrix() : rows() {}
matrix(const unsigned int nor,
const unsigned int noc,
const T val = T())
:rows(nor, vector<T>(noc, val)) {}
matrix(const matrix<T> & v)
:rows(v.rows) {}
matrix<T> & operator = (const matrix<T> & val)
{
if (&val == this) return *this;
rows = val.rows;
return *this;
}
~matrix() {}
vector<T> & operator [ ] (unsigned int i)
{ return rows[i];}
unsigned int numberOfRows() const
{ return rows.size(); }
unsigned int numberOfColumns () const
{ return rows[0].size(); }
protected:
vector<vector<T> > rows;
};
--
Charles Krug, Jr.
- Raw text -