On Jul 8, 9:05 pm, Markus Schoder <a3vr6dsg-use...@yahoo.de> 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.
|