473,597 Members | 2,190 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

STL iterators: question on how to traverse a list

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<Objob ject_list;
// populate the list with objects

for(std::list<O bj>::iterator i = object_list.beg in(); i !=
object_list.end (); i++)
{
for(std::list<O bj>::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
4 3452
In article <45************ ***********@new s.telepac.pt>,
ru********@gmai l.com says...

[ ... ]
for(std::list<O bj>::iterator i = object_list.beg in(); i !=
object_list.end (); i++)
{
for(std::list<O bj>::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
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<Objob ject_list;
// populate the list with objects

for(std::list<O bj>::iterator i = object_list.beg in(); i !=
object_list.end (); i++)
{
for(std::list<O bj>::iterator j = i; j != object_list.end (); j++)
for(std::list<O bj>::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
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<Objob ject_list;
why list? string has iterators that behave just the same
// populate the list with objects

for(std::list<O bj>::iterator i = object_list.beg in(); i !=
object_list.end (); i++)
{
for(std::list<O bj>::iterator j = i; j != object_list.end (); j++)
instead try
for(std::list<O bj>::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
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<Objob ject_list;
// populate the list with objects

for(std::list<O bj>::iterator i = object_list.beg in(); i !=
object_list.end (); i++)
{
for(std::list<O bj>::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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
2543
by: Jess Austin | last post by:
hi, I like the way that Python does lists, and I love the way it does iterators. But I've decided I don't like what it does with iterators of lists. Lists are supposed to be mutable sequences, but try to use an iterator of a list that you're mutating and watch it all fall to pieces. That is, if you change the length of a section of the list through which the iterator has already passed, it will lose track of where it is. I think...
2
1375
by: Chris Lyon | last post by:
coming from a Vb'ish sort of background, issues like iterators and list comprehensions appear at first site to be a confusion. However since I have a great deal of respect for the large brains in a jar (LBIAJ) that post on this list and construct my favourite language I realise I'm missing something. So can anyone point me at some applications, tutorials, websites and advantages that these mechanisms provide? that don't rely on having...
8
1987
by: babak | last post by:
Hi everyone I have a problem with Iterators and containers in STL that hopefully someone can help me with. This is what I try to do: I have an associative (map) container and I have a function where I with the help of iterators want to search through the container and remove a certain object in the container. Here is part of the code in that function:
14
2312
by: Jiri Kripac | last post by:
Languages such as Simula 67 contain a general concept of coroutines that allow the execution of a method to be suspended without rolling back the stack and then later resumed at the same place as it has been suspended. The C# iterators seem to be a special case of this general suspend/resume concept. The "yield" statement suspends the execution of the current method and calling MoveNext() resumes it. I think it would be cleaner to...
2
2349
by: Wavemaker | last post by:
I've been playing with C# v2.0 today, and having quite a bit of fun. The new version has added iterators. The iterators are coded directly into the class to be iterated. For example: public IEnumerable<int> Visit(SomeVisitor visitor) { int position = 0; for(int i = 0; i < Count; i++) {
8
2261
by: desktop | last post by:
In accelerated C++ on page 146 there is this example: template <class In, class Out> Out copy(In begin, In end, Out dest) { While (begin != end) *dest++ = *begin++; return dest; }
18
2101
by: desktop | last post by:
1) I have this code: std::list<intmylist; mylist.push_back(1); mylist.push_back(2); mylist.push_back(3); mylist.push_back(4);
9
2314
by: nottheartistinquestion | last post by:
As an intellectual exercise, I've implemented an STL-esque List<and List<>::Iterator. Now, I would like a signed distance between two iterators which corresponds to their relative position in the list. For instance, if I did something like distance(list.end(), list.begin()), I would get -list.size(). The STL's iterator distance function amounts to something like this: distance_type distance(Iterator first, Iterator last) {...
11
4152
by: Juha Nieminen | last post by:
Assume we have this: std::list<Typelist1(10, 1), list2(20, 2); std::list<Type>::iterator iter = list1.end(); list1.swap(list2); What happens here, according to the standard? 1) 'iter' still points to list1::end(). 2) 'iter' now points to list2::end().
0
7887
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8274
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8381
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8036
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8259
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6695
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5434
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3930
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1494
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.