Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > loop for and std::list iterator

Reply
Thread Tools

loop for and std::list iterator

 
 
raven.mp4@gmail.com
Guest
Posts: n/a
 
      07-08-2007
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?

 
Reply With Quote
 
 
 
 
Markus Schoder
Guest
Posts: n/a
 
      07-08-2007
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
 
Reply With Quote
 
 
 
 
raven.mp4@gmail.com
Guest
Posts: n/a
 
      07-08-2007
On Jul 8, 9:05 pm, Markus Schoder <(E-Mail Removed)> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Difference between Java iterator and iterator in Gang of Four Hendrik Maryns Java 18 12-22-2005 05:14 AM
How to convert from std::list<T*>::iterator to std::list<const T*>::iterator? PengYu.UT@gmail.com C++ 6 10-30-2005 03:31 AM
difference between the each iterator and the collect iterator? vasten@gmail.com Ruby 4 10-28-2005 04:42 AM
Iterator doubts, Decision on Iterator usage greg C++ 6 07-17-2003 01:26 PM



Advertisments