Hi how would you create a list of object using STL List and then iterate through each one? Thanks
list<object> Objects;for (list<object>::iterator i=Objects.begin(); i<Objects.end(); ++i){ i->Memberfunction();}
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();
}
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
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
---
John Hattan
The Code Zone
Sweet software for a saturnine world
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...
Will always work (no matter the container). However,
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
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
Popular Topics
Advertisement