• Announcements

Archived

This topic is now archived and is closed to further replies.

STL List

Recommended Posts

ANSI2000    122
Hi how would you create a list of object using STL List and then iterate through each one? Thanks

Share on other sites
johnhattan    2346
  list

Share on other sites
Guest Anonymous Poster
I just took a class on this same subject. Given a class Object, define a set as follows:

set<Object> object_set;

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

Share on other sites
ANSI2000    122
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

Share on other sites
Dactylos    122
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)

Share on other sites
johnhattan    2346
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

Share on other sites
Dactylos    122
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...

Share on other sites
Oluseyi    2103
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

Share on other sites
pirate_dau    151
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