Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterating over a QList backwards

Tags:

c++

iterator

qt

If I execute the following code:

QList<int> l;
QList<int>::const_iterator lI;

l.append(1);
l.append(2);
l.append(3);
l.append(4);
lI = l.constEnd();

while(lI != l.constBegin()) {
  std::cout << *lI << std::endl;
  --lI;
}

I get this output:

17
4
3
2

I already solved it by using the QListIterator<int>, but I really don't get why this isn't working!

Thanks in advance ...

like image 976
janr Avatar asked Dec 02 '22 19:12

janr


1 Answers

Thanks for the help, I didn't know that end() isn't pointing to the last element. Therefore, you only have to decrement before you use the node value.

while(lI != l.constBegin()) {
  --lI;
  std::cout << *lI << std::endl;
}
like image 138
janr Avatar answered Dec 12 '22 00:12

janr