I want to create a Matrix template that uses vector. For the time being I want to create the following operations. I need setting the implementation for the operations.
Maybe I do not have to use a pointer either.
template <class T>
class Matrix {
protected:
typedef vector<T>* VP;
int M; ///< Number of rows
int N; ///< Number of column
VP* MAT; ///< Matrix object (pointer to vector)
public:
////////////////////////////////////////////////
/// \brief Creates a matrix
/// Matrix b;
Matrix
(
);
////////////////////////////////////////////////
/// \brief Creates a matrix of size (m,n),
/// Matrix b(3,2);
Matrix
(
const int m,
const int n
);
////////////////////////////////////////////////
/// \brief Sets a matrix to another matrix
/// Matrix b(a);
Matrix
(
const Matrix& a
);
////////////////////////////////////////////////
/// \brief Destroys a matrix
~Matrix
(
);
};
template <class T>
inline
Matrix<T>::Matrix
(
) {
M = N = 0;
}
template <class T>
inline
Matrix<T>::Matrix
(
const int m,
const int n
) {
M = m;
N = n;
MAT = new VP[M];
for (int i = 0; i < M; i++)
{
MAT = new vector<T> (N);
}
}
template <class T>
inline
Matrix<T>::Matrix
(
const Matrix<T>& a
) {
M = a.M;
N = a.N;
MAT = new VP[M];
for (int i = 0; i < M; i++)
{
MAT = new vector<T> (N);
}
}
Why not use the already existing vector class?
That's what I want to do. I have some code that uses its own vector class. Now want to change it to use std:: vector
---------- Post updated at 10:46 AM ---------- Previous update was at 10:24 AM ----------
Have now started modifications to use vector.
Current problem is what to do instead of calling
MAT = new vector<T> (N);
template <class T>
class Matrix {
private:
std::vector< std::vector<T> > MAT;
protected:
unsigned M; ///< Number of rows
unsigned N; ///< Number of column
public:
////////////////////////////////////////////////
/// \brief Creates a matrix
/// Matrix b;
Matrix
(
);
////////////////////////////////////////////////
/// \brief Creates a matrix of size (m,n),
/// Matrix b(3,2);
Matrix
(
const int m,
const int n
);
////////////////////////////////////////////////
/// \brief Sets a matrix to another matrix
/// Matrix b(a);
Matrix
(
const Matrix& a
);
////////////////////////////////////////////////
/// \brief Destroys a matrix
~Matrix
(
);
////////////////////////////////////////////////
/// \brief Return a reference to element i in a vector
/// \param[in] i
Vector<T>&
operator []
(
const int i
);
////////////////////////////////////////////////
/// \brief Return a reference to element i in a vector
/// \param[in] i
vector<T>&
operator []
(
const int i
) const;
};
---------- Post updated at 11:01 AM ---------- Previous update was at 10:46 AM ----------
Not sure how much this will work
template <class T>
inline
Matrix<T>::Matrix
(
const unsigned m,
const unsigned n
) {
M = m;
N = n;
for (unsigned i = 0; i < M; i++)
{
MAT.resize (N);
}
}
template <class T>
inline
Matrix<T>::Matrix
(
const Matrix<T>& a
) {
M = a.M;
N = a.N;
for(int i = 0; i<M; i++)
{
vector<T> v;
for(int j = 0; j<N; j++)
{
v.push_back(a[j]);
}
MAT.push_back(v);
}
}