STL List

Started by
7 comments, last by ANSI2000 22 years, 2 months ago
Hi how would you create a list of object using STL List and then iterate through each one? Thanks
Advertisement
  list<object> Objects;for (list<object>::iterator i=Objects.begin(); i<Objects.end(); ++i){    i->Memberfunction();}  

(my byline from the Gamedev Collection series, which I co-edited) John Hattan has been working steadily in the casual game-space since the TRS-80 days and professionally since 1990. After seeing his small-format games turned down for what turned out to be Tandy's last PC release, he took them independent, eventually releasing them as several discount game-packs through a couple of publishers. The packs are actually still available on store-shelves, although you'll need a keen eye to find them nowadays. He continues to work in the casual game-space as an independent developer, largely working on games in Flash for his website, The Code Zone (www.thecodezone.com). His current scheme is to distribute his games virally on various web-portals and widget platforms. In addition, John writes weekly product reviews and blogs (over ten years old) for www.gamedev.net from his home office where he lives with his wife and daughter in their home in the woods near Lake Grapevine in Texas.

I just took a class on this same subject. Given a class Object, define a set as follows:

set<Object> object_set;

//add objects
Object t_object(initilize_values);
object_set.insert(t_object); //repeat for more objects

//access each object one at a time.
for (set<Object>::iterator iter=object_set.begin(); iter!=object_set.end(); iter++){
//iter contains address of an object in set. Dereference to use.
(*iter).doSomething();
//or
iter->doSomething();
}
Thanks...

And also to add items to the list would be something like....

class someObject
{
void someFunction();
}

void main()
{
someObject one;
someObject two;

list objects;
objects.push_back(one);
objects.push_back(two);



for(list::iterator i = objects.begin(); i < objects.end(); i++)
{
objects.someFunction();
}
}

Edited by - ANSI2000 on January 30, 2002 4:44:49 PM
quote:Original post by ANSI2000
for(list<someObject>::iterator i = objects.begin(); i < objects.end(); i++)

Never use less-than ( < ) to compare two iterators, instead use not-equal ( != ). Also, use the pre-increment operator instead of the post-increment version whenever you can, i.e. rewrite the above line as:

for(list<someObject>::iterator i = objects.begin(); i != objects.end(); ++i)
Any particular reason to prefer the not-equal to the less-than operator? Just wondering. I presume it has to do with the time it takes to resolve the operators, but I''m too lazy to look at the code.

---
John Hattan
The Code Zone
Sweet software for a saturnine world

(my byline from the Gamedev Collection series, which I co-edited) John Hattan has been working steadily in the casual game-space since the TRS-80 days and professionally since 1990. After seeing his small-format games turned down for what turned out to be Tandy's last PC release, he took them independent, eventually releasing them as several discount game-packs through a couple of publishers. The packs are actually still available on store-shelves, although you'll need a keen eye to find them nowadays. He continues to work in the casual game-space as an independent developer, largely working on games in Flash for his website, The Code Zone (www.thecodezone.com). His current scheme is to distribute his games virally on various web-portals and widget platforms. In addition, John writes weekly product reviews and blogs (over ten years old) for www.gamedev.net from his home office where he lives with his wife and daughter in their home in the woods near Lake Grapevine in Texas.

quote:Original post by johnhattan
Any particular reason to prefer the not-equal to the less-than operator? Just wondering. I presume it has to do with the time it takes to resolve the operators, but I''m too lazy to look at the code.

---
John Hattan
The Code Zone
Sweet software for a saturnine world


The elements in a list aren''t necessarily allocated at growing addresses, so comparing the iterator with less-than might cause the loop to exit prematurely (or continue for too long). Using not-equal comparison with the end-iterator will guarantee that the loop will terminate when the last iterator has been reached.

Is less-than even defined for list-iterators? Wouldn''t a true less-than comparison for a list-iterator entail searching trough the list to see if iterator1 refers to an object located before the one refered to by iterator2?

Am I thinking correctly? I suddenly became a little uncertain...
quote:Original post by Dactylos
Am I thinking correctly? I suddenly became a little uncertain...

No, you're right.

It might also help to remove the evaluation of end() outside the for loop:
std::list<SomeObject *>::iterator iter = m_list.begin(),                                  stop = m_list.end();while(iter != stop){  // blah  ++iter;}  

However, if you're performing frequent removals from the list, the original form is better. If the removals are sparse, then re-obtain stop whenever you remove a list element.

[Edit:] Forgot about HTML tag parsing...

I wanna work for Microsoft!
[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!

Edited by - Oluseyi on February 11, 2002 4:19:22 PM
Actually, I believe the best reason to use a != instead of < is because the < operator is only defined for iterators that have random access to a container. What this means is that if you wrote a loop for a vector or deque using the end condition of ''pos < objects.end()'' it would work. However if your container for objects was a map or any other non-random-access container, this would NOT work (or rather is not guaranteed to work).

To sum it up...

  for(pos = objects.begin(); pos != objects.end(); ++pos) {    ...}  


Will always work (no matter the container). However,

  for(pos = objects.begin(); pos < objects.end(); ++pos) {    ...}  


Will only work with some kinds of containers.

I hope I wasn''t too confusing. I highly recommend the book ''The C++ Standard Template Library: A Tutorial and Reference'' by Nicolai M. Josuttis . I just got it this week, and it''s a terrific read (and definitely a good reference).

Cheers!

-pirate_dau

This topic is closed to new replies.

Advertisement