473,385 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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
Mar 20 '08 #1
5 1940
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) {
// ...
}
}

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

Jason

>
Any hints?

Regards
Philip
Mar 20 '08 #2
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
Mar 20 '08 #3
ja************@gmail.com wrote:
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
Mar 20 '08 #4
On Mar 21, 9:26 am, "jason.cipri...@gmail.com"
<jason.cipri...@gmail.comwrote:
On Mar 20, 4:23 pm, "jason.cipri...@gmail.com"
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:
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.

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

Mar 23 '08 #5
On Mar 23, 2:41 am, Old Wolf <oldw...@inspire.net.nzwrote:
On Mar 21, 9:26 am, "jason.cipri...@gmail.com"

<jason.cipri...@gmail.comwrote:
On Mar 20, 4:23 pm, "jason.cipri...@gmail.com"
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:
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.
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() )
Don't forget to initialize s2 first this way:

for (s = Ships; s != Ships.end(); ++ s) {
s2 = s;
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(); )
Mar 23 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Paras | last post by:
Hi What is the correct way to delete an element from STL list while iterating through the list list<A*> _ls; A * a; list<A*>::iterator si1;
2
by: Barry Hynes | last post by:
G'Day folks, Have been working on this problem for quite some time and still no farther ahead. :( Here is my problem...bare with me i am very green :) I have to implement a Safe List,...
4
by: Christian Christmann | last post by:
Hi, I need an STL list and was thinking of putting the list in wrapper class. The reason for my decision is that you can much better perform consistency checks. For instance, I need a...
8
by: cpptutor2000 | last post by:
I am using an STL list to store data packets in a network simulator. The data packets are really C structs, which have other C structs inside them. These structs contain unsigned char arrays of...
5
by: Allerdyce.John | last post by:
Hi, I have this piece of code which loops thru a STL list, but that causs an infinite loop. bool Executer::group(MyList& bl, ResultList & grl) { for (ExecuterList::iterator i =...
7
by: Allerdyce.John | last post by:
How can I emove an item in STL iterator without use the STL algorithm? I know we can use stl erase, find_if to remove items from a STL vector , but how can I do the same without using STL...
6
by: Jonathan | last post by:
Hi. I'm having trouble figuring out what I should be doing here. I'm trying to remove an object from a list. The function is: void Alive::FromRoom () { list<Alive>::iterator iter =...
10
by: Boltar | last post by:
Hello I need to iterate through an STL list container and delete certain entries in it. I realise if use erase() with the iterator it will invalidate it but what if I store it beforehand? Ie: is...
27
by: Jason Doucette | last post by:
I'm getting an assertion fire from a list iterator being checked against NULL. This did not occur in VC++ 2003 (v7.1). Are there changes that have been made to the STL between these versions that...
12
by: Philip Mueller | last post by:
Hi, I am using multiple stl::list s of different types. Now I want to write a function list<type>::iterator s_erase(list<typel, list<type>::iterator it) that takes an iterator and deletes...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.