Giusto hai ragione...
codice:
class iterator{
friend class grid3d;
friend class const_iterator;
public:
typedef std::forward_iterator_tag iterator_category;
typedef T value_type;
typedef ptrdiff_t difference_type;
typedef T* pointer;
typedef T& reference;
iterator() : ptr(0),diz(0),diy(0),dix(0), cy(0),cx(0) {}
iterator(const iterator &other) : ptr(other.ptr) {}
iterator& operator=(const iterator &other) {
ptr = other.ptr;
diz = other.diz;
dix = other.dix;
diy = other.diy;
cy = other.cy;
cx = other.cx;
return *this;
}
~iterator() {} //distruttore iteratore
/**
* Operatore di dereferenziamento.
*
* @return il dato "puntato" dall'iteratore.
*/
reference operator*() const {
return ***ptr;
}
/**
* Operatore freccia.
*
* @return il puntatore al dato "puntato" dall'iteratore.
*/
pointer operator->() const {
return ***ptr;
}
/**
* Operatore di confronto (uguaglianza).
*
* @param other iteratore da confrontare
* @return true se i due iteratori "puntano" allo stesso dato
*/
bool operator==(const iterator &other) const {
return ptr == other.ptr;
}
/**
* Operatore di confronto (disuguaglianza).
*
* @param other iteratore da confrontare
* @return true se i due iteratori "puntano" a dati diversi
*/
bool operator!=(const iterator &other) const {
return !(*this == other);
}
/**
* Operatore di "elemento successivo". Versione pre-incremento.
*
* @return l'iteratore che "punta" all'elemento successivo
*/
iterator &operator++() {
cx++;
if(cx == dix){
**ptr = 0;
cx = 0;
cy++;
if(cy == diy){
*ptr = 0;
cy=0;
++ptr;
}else ++(*ptr);
}else ++(**ptr);
return *this;
}
/**
* Operatore di "elemento successivo". Versione post-incremento.
*
* @return l'iteratore che "punta" all'elemento successivo
*/
iterator operator++(int) {
iterator tmp(ptr,diz,diy,dix,cy,cx);
cx++;
if(cx == dix){
**ptr = 0;
cx = 0;
cy++;
if(cy == diy){
*ptr = 0;
cy=0;
++ptr;
}else ++(*ptr);
}else ++(**ptr);
return tmp;
}
private:
value_type ***ptr;
size_type diz;
size_type diy;
size_type dix;
size_type cy;
size_type cx;
/**
* Costruttore di inizializzazione. Sarà usato anche da grid3d.
*
* @param p puntatore ai dati
*/
explicit iterator(value_type ***p, size_type dz, size_type dy, size_type dx) : ptr(p),diz(dz),diy(dy),dix(dx),cy(0),cx(0) {}
explicit iterator(value_type ***p, size_type dz, size_type dy, size_type dx, size_type dyo, size_type dxo) : ptr(p),diz(dz),diy(dy),dix(dx),cy(dyo),cx(dxo) {}
};
Il codice per il test:
codice:
grid3d<int> gi(2,2,2,3);
grid3d<int> gi2(2,2,2,3);
transform(gi.begin(),gi.end(),gi2.begin(), add());
Il problema che mi sorge è molto semplice lui scorre TUTTA la matrice poi non so perché anzichè fermarsi all' end() va avanti!