[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

iterator manipulation

Posted on 2007-10-04
11
Medium Priority
?
321 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


 
0
Comment
Question by:cpeters5
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 20014979
if ((knab + 1) == disbursements.end())
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 20015003
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
 
LVL 30

Expert Comment

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


For reverse iterators, ++ actually decreases the pointer.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 53

Expert Comment

by:Infinity08
ID: 20015082
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
 

Author Comment

by:cpeters5
ID: 20015084

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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 20015115
>>>> 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
 
LVL 30

Accepted Solution

by:
Axter earned 750 total points
ID: 20015138
>>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
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 750 total points
ID: 20015144
>> 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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 20015188
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 20015192
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
 

Author Comment

by:cpeters5
ID: 20015195
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question