Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old March 20th, 2008, 09:15 PM
Philip Mueller
Guest
 
Posts: n/a
Default stl::list iterator troubles

Hi,

I have two nested for loops which look like this with my selfmade list
implementation:

for (sShip *s = Ships; s; s = s->next)
for (sShip *s2 = s->next; s2; s2 = s2->next)
{...}

Now how can I do this with stl lists and the iterators? The most
difficult thing is the start of the inner loop.
My outer loop would look like this:

for (list<cShip>::iterator iShip = Ships.begin(); iShip != Ships.end();
++iShip)

But of course I can't begin the second loop with iShip->next.....

Any hints?

Regards
Philip
  #2  
Old March 20th, 2008, 09:25 PM
jason.cipriani@gmail.com
Guest
 
Posts: n/a
Default Re: stl::list iterator troubles

On Mar 20, 4:12 pm, Philip Mueller <m...@LOESCHMICHalienemperor.de>
wrote:
Quote:
Hi,
>
I have two nested for loops which look like this with my selfmade list
implementation:
>
for (sShip *s = Ships; s; s = s->next)
for (sShip *s2 = s->next; s2; s2 = s2->next)
{...}
>
Now how can I do this with stl lists and the iterators? The most
difficult thing is the start of the inner loop.
My outer loop would look like this:
>
for (list<cShip>::iterator iShip = Ships.begin(); iShip != Ships.end();
++iShip)
>
But of course I can't begin the second loop with iShip->next.....
Something like:

list<CShip>::iterator s, s2;

for (s = Ships; s != Ships.end(); ++ s) {
s2 = s;
for (++ s2; s2 != Ships.end(); ++ s2) {
// ...
}
}

Where the ++s2 initial statement in the inner loop moves s2 to the
next element.

Jason

Quote:
>
Any hints?
>
Regards
Philip
  #3  
Old March 20th, 2008, 09:35 PM
jason.cipriani@gmail.com
Guest
 
Posts: n/a
Default Re: stl::list iterator troubles

On Mar 20, 4:23 pm, "jason.cipri...@gmail.com"
<jason.cipri...@gmail.comwrote:
Quote:
On Mar 20, 4:12 pm, Philip Mueller <m...@LOESCHMICHalienemperor.de>
wrote:
>
>
>
Quote:
Hi,
>
Quote:
I have two nested for loops which look like this with my selfmade list
implementation:
>
Quote:
for (sShip *s = Ships; s; s = s->next)
for (sShip *s2 = s->next; s2; s2 = s2->next)
{...}
>
Quote:
Now how can I do this with stl lists and the iterators? The most
difficult thing is the start of the inner loop.
My outer loop would look like this:
>
Quote:
for (list<cShip>::iterator iShip = Ships.begin(); iShip != Ships.end();
++iShip)
>
Quote:
But of course I can't begin the second loop with iShip->next.....
>
Something like:
>
list<CShip>::iterator s, s2;
>
for (s = Ships; s != Ships.end(); ++ s) {
s2 = s;
for (++ s2; s2 != Ships.end(); ++ s2) {

Also I *believe* that you can do it in one line like this:

for (s = Ships; s != Ships.end(); ++ s)
for (++(s2 = s); s2 != Ships.end(); ++ s2)
{ ... }

As long as s2 = s returns a reference to s2. I have not tested that.
Alternatively this may also work:

for (s = Ships; s != Ships.end(); ++ s)
for (s2 = s, ++ s2; s2 != Ships.end(); ++ s2)
{ ... }

You get the idea, though.

Jason
Quote:
// ...
}
>
}
>
Where the ++s2 initial statement in the inner loop moves s2 to the
next element.
>
Jason
>
>
>
Quote:
Any hints?
>
Quote:
Regards
Philip
  #4  
Old March 20th, 2008, 09:45 PM
Philip Mueller
Guest
 
Posts: n/a
Default Re: stl::list iterator troubles

jason.cipriani@gmail.com wrote:
Quote:
Also I *believe* that you can do it in one line like this:
>
for (s = Ships; s != Ships.end(); ++ s)
for (++(s2 = s); s2 != Ships.end(); ++ s2)
{ ... }
Thanks! Works fine.

Regards
Philip
  #5  
Old March 23rd, 2008, 07:45 AM
Old Wolf
Guest
 
Posts: n/a
Default Re: stl::list iterator troubles

On Mar 21, 9:26 am, "jason.cipri...@gmail.com"
<jason.cipri...@gmail.comwrote:
Quote:
On Mar 20, 4:23 pm, "jason.cipri...@gmail.com"
Quote:
On Mar 20, 4:12 pm, Philip Mueller <m...@LOESCHMICHalienemperor.de>
wrote:
Quote:
I have two nested for loops which look like this with my selfmade list
implementation:
>
Quote:
Quote:
for (sShip *s = Ships; s; s = s->next)
for (sShip *s2 = s->next; s2; s2 = s2->next)
{...}
>
Quote:
Quote:
Now how can I do this with stl lists and the iterators? The most
difficult thing is the start of the inner loop.
>
Also I *believe* that you can do it in one line like this:
>
for (s = Ships; s != Ships.end(); ++ s)
for (++(s2 = s); s2 != Ships.end(); ++ s2)
{ ... }
Yikes! This would be simpler, on the second line:
while( ++s2 != Ships.end() )

or in the version where you declare the iterators in the
scope they're used:
for ( list<foo>::iterator s2 = s; ++s2 != Ships.end(); )

  #6  
Old March 23rd, 2008, 07:55 AM
jason.cipriani@gmail.com
Guest
 
Posts: n/a
Default Re: stl::list iterator troubles

On Mar 23, 2:41 am, Old Wolf <oldw...@inspire.net.nzwrote:
Quote:
On Mar 21, 9:26 am, "jason.cipri...@gmail.com"
>
>
>
<jason.cipri...@gmail.comwrote:
Quote:
On Mar 20, 4:23 pm, "jason.cipri...@gmail.com"
Quote:
On Mar 20, 4:12 pm, Philip Mueller <m...@LOESCHMICHalienemperor.de>
wrote:
I have two nested for loops which look like this with my selfmade list
implementation:
>
Quote:
Quote:
for (sShip *s = Ships; s; s = s->next)
for (sShip *s2 = s->next; s2; s2 = s2->next)
{...}
>
Quote:
Quote:
Now how can I do this with stl lists and the iterators? The most
difficult thing is the start of the inner loop.
>
Quote:
Also I *believe* that you can do it in one line like this:
>
Quote:
for (s = Ships; s != Ships.end(); ++ s)
for (++(s2 = s); s2 != Ships.end(); ++ s2)
{ ... }
>
Yikes! This would be simpler, on the second line:
while( ++s2 != Ships.end() )
Don't forget to initialize s2 first this way:

for (s = Ships; s != Ships.end(); ++ s) {
s2 = s;
while( ++s2 != Ships.end() )
{ ... }
}
Quote:
>
or in the version where you declare the iterators in the
scope they're used:
for ( list<foo>::iterator s2 = s; ++s2 != Ships.end(); )
 

Bookmarks

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles