We help IT Professionals succeed at work.

iterator manipulation

cpeters5
cpeters5 asked
on
344 Views
Last Modified: 2008-01-09
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

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
AxterSenior 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.

Author

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
>>>> 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)
UNLOCK SOLUTION
CERTIFIED EXPERT
Top Expert 2009
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
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 ?

Author

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.

Get Access
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

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

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

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
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

Ask ANY Question

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

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.