We help IT Professionals succeed at work.

# iterator manipulation

on
344 Views
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

Comment
Watch Question

## View Solutions Only

Commented:
if ((knab + 1) == disbursements.end())

Commented:
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
Senior Software Engineer
CERTIFIED EXPERT

Commented:
The for loop should be using incrementation.
Example:
for (knab=disbs.rbegin(); knab!= disbs.rend(); knab++) {

For reverse iterators, ++ actually decreases the pointer.
CERTIFIED EXPERT
Top Expert 2009

Commented:
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.

Commented:

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

Commented:
>>>> 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).

Senior Software Engineer
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
CERTIFIED EXPERT
Top Expert 2009
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)

Commented:
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
CERTIFIED EXPERT
Top Expert 2009

Commented:
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 ?

Commented:
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

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

###### Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

Deciding to stick with EE.

Mohamed Asif

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
###### Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Connect with Certified Experts to gain insight and support on specific technology challenges including:

• Troubleshooting
• Research
• Professional Opinions
Unlock the solution to this question.