• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 326
  • Last Modified:

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


 
0
cpeters5
Asked:
cpeters5
  • 4
  • 3
  • 2
  • +1
2 Solutions
 
itsmeandnobodyelseCommented:
if ((knab + 1) == disbursements.end())
0
 
itsmeandnobodyelseCommented:
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
0
 
AxterCommented:
The for loop should be using incrementation.
Example:
  for (knab=disbs.rbegin(); knab!= disbs.rend(); knab++) {


For reverse iterators, ++ actually decreases the pointer.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
Infinity08Commented:
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.
0
 
cpeters5Author 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
0
 
itsmeandnobodyelseCommented:
>>>> 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).

0
 
AxterCommented:
>>What would be the correct conditional statement for testing if knab isreaching the last node?

Don't try to compare your reverse iterator with ::begin.
begin uses a normal iterator, which may not be compatible with a reverse iterator.

To test for the last (Or first item), test it against rend, as in Infinity08's example:
if ((knab + 1) == disbs.rend())

Also make sure to use ++ instead of --, as  I described in my previous post.
0
 
Infinity08Commented:
>> The if (knab == disbs.begin()) is correct if you want to test whether you reached the index 0.

Why not do it consistently, and use rend() to test like I showed ? (since you're doing a reverse iteration)
0
 
itsmeandnobodyelseCommented:
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
0
 
Infinity08Commented:
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 ?
0
 
cpeters5Author 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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now