By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,022 Members | 856 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,022 IT Pros & Developers. It's quick & easy.

STL iterators: question on how to traverse a list

P: n/a
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<Objobject_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
--
Running Kubuntu 6.06 with KDE 3.5.4 and proud of it.
jabber:ru********@jabber.org
Sep 17 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
In article <45***********************@news.telepac.pt>,
ru********@gmail.com says...

[ ... ]
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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 17 '06 #2

P: n/a
Rui Maciel wrote:
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<Objobject_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++)
for(std::list<Obj>::iterator j = i, ++j; 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.
Sep 17 '06 #3

P: n/a
Rui Maciel wrote:
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<Objobject_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
for(std::list<Obj>::iterator j = (i+1); j !=
object_list.end(); j++)
{
// 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

Sep 18 '06 #4

P: n/a
Rui Maciel wrote:
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<Objobject_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

Sep 18 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.