I'm writing a class "translator" and I need two iterators, one constant and other more normal. This is the interesting zone of code:
...
iterator begin() {
iterator i;
i.puntero=palabras.begin();
return i;
}
iterator end(){
iterator i;
i.puntero=palabras.end();
return i;
}
...
const_iterator begin() const {
const_iterator i;
i.puntero=palabras.begin();
return i;
}
const_iterator end() const {
const_iterator i;
i.puntero=palabras.end();
return i;
}
...
There are more code but I think that isn't interesting.
Well, I need use the iterators in a for loops and is in this moment when the code fail:
#Work fine
for(Traductor::iterator it=traductor.begin(); it!=traductor.end(); ++it)
cout << it->first << endl;
#Don't work
for (Traductor::const_iterator it=traductor.begin(); it!=traductor.end(); ++it)
cout << it->first << endl;
The error is:
error: conversion from ‘Traductor::iterator’ to non-scalar type ‘Traductor::const_iterator’ requested for (Traductor::const_iterator it=traductor.begin(); it!=traductor.end(); ++it)
And i don't know how make to the program uses the const_iterator in the second loop and don't try to use the normal.
Any idea? Thanks you so much.
Solution adopted:
Finally, If I use a const traductor
isn't necessary to changing the name to end
and begin
methods but if traductor
isn't not constant i need use cend
and cbegin
methods.
Example:
#When is constant:
const Traductor tradConst = traductor;
for (Traductor::const_iterator it=tradConst.begin(); it!=tradConst.end(); ++it)
#When isn't, use diferent methods to differentiate:
//for (Traductor::const_iterator it=traductor.cbegin(); it!=traductor.cend(); ++it)
...
So, I have:
const_iterator begin() const {...}
const_iterator end() const {...}
iterator begin() {...}
iterator end(){...}
Useful when I use constant and non-constant objects, and when I want use const and non-const iterator in a non-const object I will change the name of the methods:
const_iterator cbegin() const {...}
const_iterator cend() const {...}
iterator begin() {...}
iterator end(){...}
Thanks you so much guys!