On Mar 20, 4:23 pm, "jason.cipri...@gmail.com"
<jason.cipri...@gmail.comwrote:
On Mar 20, 4:12 pm, Philip Mueller <m...@LOESCHMICHalienemperor.de>
wrote:
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) {
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
// ...
}
}
Where the ++s2 initial statement in the inner loop moves s2 to the
next element.
Jason
Any hints?
Regards
Philip