mercredi 22 janvier 2020

Return struct from iterator forward

The project requires the design and implementation of a class that implements a sparse matrix (SparseMatrix) of generic elements T. Class must include support for iterators (reading AND writing) forward type to access only the elements inserted in the matrix. The iterator must return a struct element that contains only data and in particular the coordinates (i, j) which must not be modifiable and the value entered in that position. Items must be returned based on their logical position in the matrix, from left to right and from top to bottom (increasing coordinates). I tried with opeator*.


template <class T>
class SparseMatrix
{
    private:

    struct matrice
    {
        T val;
        int colonna;
        int riga;

        matrice *next;

        matrice() : next(0) {}

        matrice(const T &v,int r,int c, matrice* n = 0): val(v), riga(r),colonna(c), next(n){}
    };

....
class iterator 
        {
            struct element
                {
                    T v;
                    int col;
                    int rig;

                };


        public: ...

element& operator*() const 
            {
                element *e;
                e->v = n->val;
                e->col = n->colonna;
                e->rig = n->riga;
                return *e; 
            } 

Aucun commentaire:

Enregistrer un commentaire