c++ - Iterating over a QList backwards -
if execute 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 output:
17 4 3 2
i solved using qlistiterator<int>
, don't why isn't working!
thanks in advance ...
try
li = l.constend() - 1;
i'm not sure if solves problem, far know, end iterators point 1 past end of container.
i wanted address concern in comments. when this:
li = l.constend(); while(li != l.constbegin()) { std::cout << *li << std::endl; --li; }
you start off end of container, , loop never reaches constbegin
. that's because when decrement, li
becomes constbegin
, while loop doesn't execute. (that's why 1 never outputted.)
but if do:
li = l.constbegin(); while(li != l.constend()) { std::cout << *li << std::endl; ++li; }
the same thing happens, except loop terminates once reaches constend
. logically makes sense, if constend
didn't point past end of container, cut off , not output 4.
Comments
Post a Comment