STL iterators: question on how to traverse a list

R

Rui Maciel

I'm trying to go through the elements of a STL list container in such a way
that each element can be acessed along with each and every subsequent
element from the list. For example, let's say I have the following list:

A B C D E F G H...

What I am trying to do is something like this:

AB, AC, AD, AE, ... , BC, BD, BE, ... , CD, CE, ...

I tried to do that with a couple of nested for(;;) loops abut I wasn't
successfull. What I tried to do was something like this....

<code>
std::list<Obj> object_list;
// populate the list with objects

for(std::list<Obj>::iterator i = object_list.begin(); i !=
object_list.end(); i++)
{
for(std::list<Obj>::iterator j = i; j != object_list.end(); j++)
{
// do stuff with i and j
}
}
</code>

That starts with i and j pointing towards the same object, which isn't good.
I've tried to use something like j++ = i but I haven't had much luck.
Finally I used a if(i == j) return; but I do not believe that this is the
best way of doing things.

So, how can I traverse this list? What is the best way of doing things?


Thanks in advace
Rui Maciel
 
J

Jerry Coffin

[ ... ]
for(std::list<Obj>::iterator i = object_list.begin(); i !=
object_list.end(); i++)
{
for(std::list<Obj>::iterator j = i; j != object_list.end(); j++)
{
// do stuff with i and j
}
}
</code>

That starts with i and j pointing towards the same object, which isn't good.
I've tried to use something like j++ = i but I haven't had much luck.

Try 'j=i+1'. Depending on what "stuff" you do with i and j, you might be
able to us an algorithm instead, but without knowing what you're doing,
it's hard to say what algorithm or what you'd do with it.
 
G

Gianni Mariani

Rui said:
I'm trying to go through the elements of a STL list container in such a way
that each element can be acessed along with each and every subsequent
element from the list. For example, let's say I have the following list:

A B C D E F G H...

What I am trying to do is something like this:

AB, AC, AD, AE, ... , BC, BD, BE, ... , CD, CE, ...

I tried to do that with a couple of nested for(;;) loops abut I wasn't
successfull. What I tried to do was something like this....

<code>
std::list<Obj> object_list;
// populate the list with objects

for(std::list<Obj>::iterator i = object_list.begin(); i !=
object_list.end(); i++)
{
for(std::list<Obj>::iterator j = i; j != object_list.end(); j++)

{
// do stuff with i and j
}
}
</code>

BTW - it's probably a good idea to get into the habbit of using the
pre-increment operator if all you want to do is increment (the same for
decrement). In this case, it avoids creating a temporary iterator.
That starts with i and j pointing towards the same object, which isn't good.
I've tried to use something like j++ = i but I haven't had much luck.
Finally I used a if(i == j) return; but I do not believe that this is the
best way of doing things.

So, how can I traverse this list? What is the best way of doing things?

Also, a suggestion; if possible, post a complete (including main) chunk
o code.
 
D

dasjotre

Rui said:
I'm trying to go through the elements of a STL list container in such a way
that each element can be acessed along with each and every subsequent
element from the list. For example, let's say I have the following list:

A B C D E F G H...

What I am trying to do is something like this:

AB, AC, AD, AE, ... , BC, BD, BE, ... , CD, CE, ...

you want to enumerate all 2 element combinations over a set of leters?
I tried to do that with a couple of nested for(;;) loops abut I wasn't
successfull. What I tried to do was something like this....

<code>
std::list<Obj> object_list;

why list? string has iterators that behave just the same
// populate the list with objects

for(std::list<Obj>::iterator i = object_list.begin(); i !=
object_list.end(); i++)
{
for(std::list<Obj>::iterator j = i; j != object_list.end(); j++)
instead try
{
// do stuff with i and j
one enumeration is (*i)(*j)
}
}
</code>
So, how can I traverse this list? What is the best way of doing things?

this might not be the 'best' way but it seems correct for 2 element
combinations
 
M

Michiel.Salters

Rui said:
I'm trying to go through the elements of a STL list container in such a way
that each element can be acessed along with each and every subsequent
element from the list. For example, let's say I have the following list:

A B C D E F G H...

What I am trying to do is something like this:

AB, AC, AD, AE, ... , BC, BD, BE, ... , CD, CE, ...

I tried to do that with a couple of nested for(;;) loops abut I wasn't
successfull. What I tried to do was something like this....

<code>
std::list<Obj> object_list;
// populate the list with objects

for(std::list<Obj>::iterator i = object_list.begin(); i !=
object_list.end(); i++)
{
for(std::list<Obj>::iterator j = i; j != object_list.end(); j++)
{
// do stuff with i and j
}
}
</code>

That starts with i and j pointing towards the same object, which isn't good.
I've tried to use something like j++ = i but I haven't had much luck.

You obviously want j = next(i). Boost has an implementation, but
the basic form is trivial:

template <typename IT>
IT next (IT curr) // by-value => copy
{
return ++curr;
}

HTH,
Michiel Salters
 

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

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top