iterators to a list of class pointers ...
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
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.
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.
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?
if i read your original post right, you are looking for this:
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.
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.
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?
(*iter)->someFunc ();
so does the iterator type overload unary * to defrence itself? is this what is happening?
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;}
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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement