cpeters5
asked on
iterator manipulation
I have a question concerning iterators. From the following code
AReversedIterator knab;
for (knab=disbs.rbegin(); knab!= disbs.rend(); knab--) {
//do something
if (knab == disbursements.begin())
//do something else
}
Apparently, the "==" is not correctly used in this case.
What would be the correct conditional statement for testing if knab isreaching the last node?
Thanks,
pac
AReversedIterator knab;
for (knab=disbs.rbegin(); knab!= disbs.rend(); knab--) {
//do something
if (knab == disbursements.begin())
//do something else
}
Apparently, the "==" is not correctly used in this case.
What would be the correct conditional statement for testing if knab isreaching the last node?
Thanks,
pac
if ((knab + 1) == disbursements.end())
Note, the iterator type has all operators ++ and operator + well-defined. As 'knab' is a valid iterator within the loop it is different to disbursements.end(). So, (knab + 1) is a valid iterator or equal to disbursements.end().
Regards, Alex
Regards, Alex
The for loop should be using incrementation.
Example:
for (knab=disbs.rbegin(); knab!= disbs.rend(); knab++) {
For reverse iterators, ++ actually decreases the pointer.
Example:
for (knab=disbs.rbegin(); knab!= disbs.rend(); knab++) {
For reverse iterators, ++ actually decreases the pointer.
Since this is a reverse iteration, you need to use :
AReversedIterator knab;
for (knab = disbs.rbegin(); knab != disbs.rend(); ++knab) {
//do something
if ((knab + 1) == disbs.rend())
//do something else
}
ie. ++ instead of --, and a modified if statement.
AReversedIterator knab;
for (knab = disbs.rbegin(); knab != disbs.rend(); ++knab) {
//do something
if ((knab + 1) == disbs.rend())
//do something else
}
ie. ++ instead of --, and a modified if statement.
ASKER
I am sorry, ther is a typo in hte snippet. The disb and disbursement are the same thing.
AReversedIterator knab;
for (knab=disbs.rbegin(); knab!= disbs.rend(); knab--) {
//do something
if (knab == disbs.begin())
//do something else
}
Apparently, the "==" is not correctly used in this case.
What would be the correct conditional statement for testing if knab isreaching the last node?
Thanks,
pac
>>>> Apparently, the "==" is not correctly used in this case.
The if (knab == disbs.begin()) is correct if you want to test whether you reached the index 0. But the knab-- is not correct cause you need to 'increment' a reverse_iterator (not decrement).
The if (knab == disbs.begin()) is correct if you want to test whether you reached the index 0. But the knab-- is not correct cause you need to 'increment' a reverse_iterator (not decrement).
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
IMO reverse iteration is somehow strange because of the 'increment' operator to use.
You may consider using a forward iterator instead:
AForwarditerator knab = disbs.end();
while (knab != disbs.begin())
{
--knab;
if (knab == disbs.begin())
...
}
or an indexed access
AForwarditerator knab;
for (int i = (int)disbs.size()-1, i >= 0; --i)
{
knab = disbs.begin() + i;
if (i == 0)
....
}
Regards, Alex
You may consider using a forward iterator instead:
AForwarditerator knab = disbs.end();
while (knab != disbs.begin())
{
--knab;
if (knab == disbs.begin())
...
}
or an indexed access
AForwarditerator knab;
for (int i = (int)disbs.size()-1, i >= 0; --i)
{
knab = disbs.begin() + i;
if (i == 0)
....
}
Regards, Alex
I see that you gave a B grade. That usually means that the question wasn't answered completely. Which part is still unclear to you ? Can we assist you further ?
ASKER
Thanks infinity08, itsme and Axter. You guys are really great help!
After assinging points, I realized that I missed Itsme :-(. I will keep this in mind for the next question.
pac
After assinging points, I realized that I missed Itsme :-(. I will keep this in mind for the next question.
pac