increment iterator after loop

This is a discussion on increment iterator after loop within the C++ Programming forums, part of the General Programming Boards category; I have a list interator Code: list<ClassA*>::iterator it; for(it = list1.begin(); it != list1.end(); ++it) { if( (*it)->itsvalB == myvalB) ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    24

    increment iterator after loop

    I have a list interator

    Code:
    list<ClassA*>::iterator it;
    for(it = list1.begin(); it != list1.end(); ++it)
    {
        if( (*it)->itsvalB == myvalB)
         {
           break;
           }
    }
      if(it == list1.begin())
       { do A; }
        if(it == list1.end())
       { do B; }
        else
       { list<ClassA*>::iterator it1 = ++it;
          cout<<(*it1)->itsvalB<<endl;
       }
    this will not increment to the next val in list1. how can i do such a thing?
    I don't want to increment it1 in the for loop, because then it1 will increment past the end of the loop for the last increment (i think).

    Many thanks for the help again.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    It will increment unless that if statement evaluates to true and you break out of the loop.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Except of course it does. Why do you think it doesn't?

  4. #4
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    I don't want to increment it1 in the for loop, because then it1 will increment past the end of the loop for the last increment (i think).
    How about something like -

    Code:
    if(it != list1.end())
    {
       list<ClassA*>::iterator it1 = ++it;
          cout<<(*it1)->itsvalB<<endl;
    }
    Spidey out!

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    How about something like -
    Except for the problem where that code will crash if it points to the last item in the list...

  6. #6
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    It may also just corrupt some memory. Like the new/malloc pool.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 11:44 AM
  2. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 01:38 PM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 01:13 AM
  4. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 07:19 AM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 04:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21