I'd like to create a matrix class using the vector library. I have seen many examples of creating a matrix class using standard C arrays, and in each of these, arrays are declared in the private section of the class declaration, and initialized in the constructor function, as such:

```
template <typename Type>
class matrix
{
private:
Type * M;
// matrix size
int nrow;
int ncol;
public:
matrix(int m, int n, int val= 0);
};
template <typename Type>
matrix<Type>::matrix(int m, int n)
{
M = new double[m*n];
nrow = m;
ncol = n;
}
```

So here the array is kept in the free store.

My class, instead, uses vectors. As of now, it looks something like:

```
template <typename Type>
class matrix
{
private:
// create a single vector to hold the entire matrix:
// a two-dimensional vector makes it difficult to work with
// whole columns.
std::vector<Type> M;
// matrix size
int nrow;
int ncol;
public:
matrix(int m, int n, int val= 0);
Type &operator()(int i, int j);
Type operator()(int i, int j) const;;
};
template <typename Type>
matrix<Type>::matrix(int m, int n, int val)
{
// default matrix is 1x1, filled with 0.
M.resize(m*n,val);
nrow = m;
ncol = n;
}
```

But I feel that this is abusing memory quite a bit. Is it advisable to use the free store to house the vector? I have tried doing something like:

```
template <typename Type>
class matrix
{
private:
// create a single vector to hold the entire matrix:
// a two-dimensional vector makes it difficult to work with
// whole columns.
std::vector<Type> M;
// matrix size
int nrow;
int ncol;
public:
matrix(int m, int n, int val= 0);
Type &operator()(int i, int j);
Type operator()(int i, int j) const;;
};
template <typename Type>
matrix<Type>::matrix(int m, int n, int val)
{
M = new std::vector<Type>();
// default matrix is 1x1, filled with 0.
M.resize(m*n,val);
nrow = m;
ncol = n;
}
```

But this naive approach doesn't work. My feeling is that it might not be necessary, or even possible, to use the new operator with vectors, but I know not near enough C++ to make a sound conclusion.