Jump to content
  • Advertisement
Sign in to follow this  
ekrax

iterators to a list of class pointers ...

This topic is 5145 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

for some reason i cannot figure this out, i could just use a vector but this is driving me crazy (i'm probably missing something stupid again ... i googled but only found stuff on sorting a list of pointers) anyways ... for example ... std::vector < myClass * > v; for (int n = 0; n < v.size (); n ++) { v[n] -> memberFunction_01; v[n] -> memberFunction_02; } now this is where i'm stuck ... since lists are liner i can't use [] ... so how the heck do you do this? im stumped on the syntax ... std::list < myClass * > l; std::list < myClass * > :: iterator i = l.begin (); while (i != l.end ()) { i -> ... well then ... } well i guess i could overload [] operator to make my list like a vector ... can you do this? or doing something like this just reserved for vectors? thanks for any help

Share this post


Link to post
Share on other sites
Advertisement
augh, sorry I misread that, ignore that post..
unless you overload the operators if you want to index into a specific one, that'll be the only way (it'll still have to traverse down the nodes though). Vectors are fine, just use .reserve() to avoid having the data relocate itself.

Share this post


Link to post
Share on other sites
yeah i wasn't sure you could index an iterator to a list, but it looked like it was right ... so basically there is no solution haha ... so it looks like i have to use a vector to do this ... which brings me to conclude why use a list when you can use a vector?

Share this post


Link to post
Share on other sites
if i read your original post right, you are looking for this:

std::list<Whatever*> things;
for (std::list<Whatever*>::iterator iter = things.begin(); iter != things.end(); iter++) {
(*iter)->MemberFunction();
}


You cannot index into the list. Arrays and vectors work off the fact that the memory in those arrays or vectors are all in one block. A std::list is a linked list whereas a vector is more like an array.

Vectors suffer from the fact that they must keep that memory in order. A linked list does not care. So every time you add to or erase a vector element from the middle of the vector, everything after that element has to get shuffled back or forward to keep everything contiguous. A list would simply have to relink.

Use a vector if insertion or deletion in the beginning or middle is not required much but retrieval speed is needed. Use a list if retrieval speed is not a big deal but you do a lot of insertion and deletion from everywhere in the list.

Example: my game has a list of objects in the game that need per-frame updating. I use a std::list for that since when i iterate over them, i do so in order every frame and i never need random access to the elements, and also because i add and remove things from the list constantly. I would not dare use a vector for that, especially as there might be thousands of things in the list.

Share this post


Link to post
Share on other sites
ok that makes sense, this is exactly what im applying this to, i have a large list of refrences to differnt objects all of same base class, since i will have to insert and delete objects from that list, using a list would be much better ...

(*iter)->someFunc ();

so does the iterator type overload unary * to defrence itself? is this what is happening?

Share this post


Link to post
Share on other sites
Quote:
Original post by ekrax
so does the iterator type overload unary * to defrence itself? is this what is happening?


For list yes the iterator type for list overloads the dereference operator, all user-defined iterators are written to act like a pointers so not only can use library containers with algorithms that take iterators you can also use pointers to arrays aswell, this is static polymoprhism through templates example:


#include <iterator>
#include <algorithm>
#include <iostream>

int main() {

int A[] = { 1, 4, 2, 8, 5, 7 };
const int N = sizeof(A) / sizeof(int);
std::sort(A, A + N);
std::copy(A, A + N, std::ostream_iterator<int>(std::cout, ", "));

return 0;
}


Share this post


Link to post
Share on other sites
Quote:
Original post by ekrax
so does the iterator type overload unary * to defrence itself? is this what is happening?


Yes, basically. It derefernces the iterator, not the pointer it iterates to. Keep in mind that it may not even be a pointer that you are containing in the list.

(*iter)->Function();

// call the function of a pointer held by the iterator


iter->Function();

// call the function of the iterator

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!