Hi, i'm making a matrix class, here is my code:
#ifndef MATRICE_H
#define MATRICE_H
#include <iostream>
template <class T> class matrice {
public:
typedef int sizet;
typedef T valuet;
matrice(): _data(0), _row(0), _col(0) {}; //costruttore default
matrice(sizet rw, sizet cl) { //causa errore in valgrind
_data=new valuet*[rw];
for(sizet i=0;i<rw;i++)
_data[i]=new valuet[cl];
_row=rw;
_col=cl;
}
matrice(sizet rw, sizet cl, valuet value) {
_data=new valuet*[rw];
for(sizet i=0;i<rw;i++) {
_data[i]=new valuet[cl];
for(sizet j=0;j<cl;j++)
_data[i][j]=value;
}
_row=rw;
_col=cl;
}
matrice(const matrice &other) { //copy constructor
_row=other._row;
_col=other._col;
_data=new valuet*[_row];
for(sizet i=0;i<_row;i++) {
_data[i]=new valuet[_col];
for(sizet j=0;j<_col;j++)
_data[i][j]=other._data[i][j];
}
}
~matrice() { //distruttore
for (sizet i=0;i<_row;i++)
delete[] _data[i];
delete[] _data;
_data=0;
}
matrice& operator=(const matrice &other){ //operatore =
if (this!=&other) {
for (sizet i=0;i<_row;i++)
delete[] _data[i];
delete[] _data;
_row=other._row;
_col=other._col;
_data=new int*[_row];
for(sizet i=0;i<_row;i++) {
_data[i]=new int[_col];
for(sizet j=0;j<_col;j++)
_data[i][j]=other._data[i][j];
}
}
return *this;
}
sizet getSize() const {
return _row*_col;
}
sizet getRow() const {
return _row;
}
sizet getCol() const {
return _col;
}
valuet getValue(sizet x, sizet y) const {
return _data[x][y];
}
void setValue(sizet x, sizet y, valuet value) {
_data[x][y]=value;
}
valuet &operator()(sizet x, sizet y) { //lettura e scrittura
return _data[x][y];
}
const valuet &operator()(sizet x, sizet y) const { //solo lettura
return _data[x][y];
}
private:
valuet **_data;
sizet _row;
sizet _col;
};
struct is_even {
inline bool operator()(const int value) const {
return value%2==0 ? true : false;
}
};
struct is_letter {
inline bool operator()(const char value) const {
return (value>='a' && value<='z') || (value>='A' && value<='Z') ? true : false;
}
};
template <class T>
std::ostream &operator<<(std::ostream &left, const matrice<T> &right) {
for(typename matrice<T>::sizet i=0;i<right.getRow();i++){
for(typename matrice<T>::sizet j=0;j<right.getCol();j++)
left<<right.getValue(i,j)<<" ";
left<<std::endl;
}
return left;
}
template <class T, class F>
bool verify(const matrice<T> &mat, const F &fun) {
bool ret=true;
for(typename matrice<T>::sizet i=0;i<mat.getRow();i++)
for(typename matrice<T>::sizet j=0;j<mat.getCol();j++)
ret=ret && fun(mat.getValue(i,j));
return ret;
}
#endif
This is a project for school, now is required to implement an iterator that goes through every element of the matrix (first all the element of the first row, then all the element of the second row, and so on)
i made a class iterator with the ++ operator that just increment the pointer, while begin() return the first element of the matrix and end() the last, but doing so it reads all the element of the matrix but also other data between the rows, how can i fix it?
please help me!
ps: sorry for my english but i'm italian!