I am currently programming a template matrix.
It contains a vector<vector<T>>
called mat and cols and rows vals that contain the number of rows and columns.
I tried to build an iterator, and found out that I cannot build an iterator function for vector of vector. since the rest of my code was already written, i added a matrixToVector function which turn my vector<vector<T>>
to vector<T>
(I know it's not the best option, but it's just for a college exercise).
On my windows laptop in tuns great, but on the linux computer labs the first two number of the iterator are always a very large random number and then 0 and then the rest of the numbers are fine.
Here is the code:
/**
* turns the 2d mat vecor to 1d vector.
*/
vector<T> matrixToVector()
{
vector<T> v;
for(unsigned int i = 0 ; i < rowsNum; i++)
{
for(unsigned int j = 0; j < colsNum; j++)
{
v.push_back(mat[i][j]);
}
}
return v;
}
/**
* iterator
*/
typedef typename std::vector<T>::const_iterator const_iterator;
/**
* return the end of the iterator.
*/
const_iterator end()
{
return matrixToVector().end();
}
/**
* return the begining of the iterator.
*/
const_iterator begin()
{
return matrixToVector().begin();
}
I don't have a clue what's wrong. What should I do?
EDIT: When I print the matrix with the regular printing function, it's works great on Linux and Windows.