loop for and std::list iterator

Discussion in 'C++' started by raven.mp4@gmail.com, Jul 8, 2007.

  1. Guest

    Hi,
    I have a little question about std::list iterators. Take a look at
    this piece of code:

    list<Type*>::iterator it = myList.begin();

    Type *p;

    for(it; it != myList.end(); ++it)
    {
    p = (*it);

    if(...)
    {
    myList.pop_front();
    InsertIntoList(p); // inserts p into new location
    it = myList.begin();
    }
    else
    break;
    }

    Why 'it = myList.begin()' doesn't set 'it' to the beginning of the
    myList?
     
    , Jul 8, 2007
    #1
    1. Advertising

  2. On Sun, 08 Jul 2007 11:15:24 -0700, raven.mp4 wrote:
    > Hi,
    > I have a little question about std::list iterators. Take a look at this
    > piece of code:
    >
    > list<Type*>::iterator it = myList.begin();
    >
    > Type *p;
    >
    > for(it; it != myList.end(); ++it)
    > {
    > p = (*it);
    >
    > if(...)
    > {
    > myList.pop_front();
    > InsertIntoList(p); // inserts p into new location
    > it = myList.begin();
    > }
    > else
    > break;
    > }
    >
    > Why 'it = myList.begin()' doesn't set 'it' to the beginning of the
    > myList?


    It does. Then "it" gets incremented (possibly invoking undefined
    behaviour if the list is empty) and then the next loop iteration begins
    with "it" pointing to the second element in the list.

    It is difficult to tell but it almost looks like you rather wanted

    while(!myList.empty())
    {
    it = myList.begin();
    p = *it;

    if(...)
    {
    myList.pop_front();
    InsertIntoList(p); // inserts p into new location
    }
    else
    break;
    }

    --
    Markus Schoder
     
    Markus Schoder, Jul 8, 2007
    #2
    1. Advertising

  3. Guest

    On Jul 8, 9:05 pm, Markus Schoder <> wrote:
    > On Sun, 08 Jul 2007 11:15:24 -0700, raven.mp4 wrote:
    > > Hi,
    > > I have a little question about std::list iterators. Take a look at this
    > > piece of code:

    >
    > > list<Type*>::iterator it = myList.begin();

    >
    > > Type *p;

    >
    > > for(it; it != myList.end(); ++it)
    > > {
    > > p = (*it);

    >
    > > if(...)
    > > {
    > > myList.pop_front();
    > > InsertIntoList(p); // inserts p into new location
    > > it = myList.begin();
    > > }
    > > else
    > > break;
    > > }

    >
    > > Why 'it = myList.begin()' doesn't set 'it' to the beginning of the
    > > myList?

    >
    > It does. Then "it" gets incremented (possibly invoking undefined
    > behaviour if the list is empty) and then the next loop iteration begins
    > with "it" pointing to the second element in the list.
    >
    > It is difficult to tell but it almost looks like you rather wanted
    >
    > while(!myList.empty())
    > {
    > it = myList.begin();
    > p = *it;
    >
    > if(...)
    > {
    > myList.pop_front();
    > InsertIntoList(p); // inserts p into new location
    > }
    > else
    > break;
    >
    > }
    >
    > --
    > Markus Schoder


    Oh, now I see my mistake. Thank you for explanation.
     
    , Jul 8, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vivi Orunitia
    Replies:
    11
    Views:
    4,527
    Martijn Lievaart
    Feb 4, 2004
  2. Replies:
    6
    Views:
    681
    Jim Langston
    Oct 30, 2005
  3. David Bilsby
    Replies:
    5
    Views:
    2,078
    David Bilsby
    Oct 9, 2007
  4. Juha Nieminen
    Replies:
    22
    Views:
    1,062
    Kai-Uwe Bux
    Oct 12, 2007
  5. Isaac Won
    Replies:
    9
    Views:
    419
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page