mardi 8 décembre 2015

How can I use const_iterator correctly?

I am trying to make a container class with map from stl lib, but I have serious problem with const_iterator.

In my class ConjuntoPreguntas how you can appreciate, I have tryed to use diferent format to begin() and end() methods, but I don't find the correct way.

In my main class I tried to use imprimir function, that receive const ConjuntoPreguntas, and there is the problem. So I need to use const ConjuntoPreguntas&.

enter code here

void imprimir( const ConjuntoPreguntas& cp ){ ConjuntoPreguntas::const_iterator it= cp.cbegin();

};

int main(){ ConjuntoPreguntas CP;

ifstream fin("PreguntasConstitucion.txt"); 

fin >> CP;

imprimir(CP);

} class ConjuntoPreguntas{

private:
    map<int,Pregunta> preguntas;

public:
        class const_iterator; /
        class iterator{
         private:
          map<int,Pregunta>::iterator it;


       public:
           iterator & operator++(){
            ++it;
           }

           iterator & operator--(){
           --it;
           }

           pair<const int,Pregunta> &operator *(){
          return *it;
           }

           bool operator ==(const iterator &i){
          return i.it==it;
           }      

           bool operator !=(const iterator &i){
          return i.it!=it;
           }

           friend class ConjuntoPreguntas;
             friend class const_iterator;
        };

        /**
         * @brief clase para iterar sobre la guia
         **/
        class const_iterator{
         private:
          map<int,Pregunta>::iterator it;
         public:

           const_iterator & operator++(){
            ++it;
           }

           const_iterator & operator--(){
           --it;
           }

           pair<const int,Pregunta> &operator *(){
          return *it;
           }

           bool operator ==(const const_iterator &i){
          return i.it==it;
           }      

           bool operator !=(const const_iterator &i){
          return i.it!=it;
           }


           friend class ConjuntoPreguntas;

        };

        /**
         * @brief Inicializa un iterator al comienzo de la guia
         * */
       **iterator begin(){
            iterator i;
            i.it=preguntas.begin();
            return i;
        }

        const_iterator cbegin(){
            const_iterator i ; 
            i.it = preguntas.begin();
            return i ;
        }
        /**
        const_iterator cbegin(){
            const_iterator i ; 
            i.it = preguntas.begin();
            return i ;
        }
        */
        /**
         * @brief Inicializa un iterator al final de la guia
         * */
       iterator end(){
            iterator i;
            i.it=preguntas.end();
            return i;
        } 

        const_iterator cend(){
            const_iterator i;
            i.it= preguntas.end();
            return i;
        }
        /* const_iterator cend(){
            const_iterator i;
            i.it= preguntas.end();
            return i;
        }
         */**

};

Aucun commentaire:

Enregistrer un commentaire