stl::list iterator troubles

P

Philip Mueller

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
 
J

jason.cipriani

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
 
J

jason.cipriani

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
 
P

Philip Mueller

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
 
O

Old Wolf

On Mar 20, 4:23 pm, "(e-mail address removed)"

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(); )
 
J

jason.cipriani

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() )
{ ... }
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top