Page 1 of 1

C++ STL Containers

Posted: Fri Oct 03, 2003 9:46 am
by sonneveld
Hi!

I'm playing around with the STL library and trying to create a vector container of classes:

Code: Select all

class MyClass
{
....
};


class MyClassVector: public vector<MyClass>
{

};
Now, i'm trying to create a GUI listbox, populating it with data from the vector. For each item in the listbox, I want to have a pointer to the specific class in the vector. That way, if an event occurs for that item in the listbox, i can get more information from the class.

Now, as I understand it, an iterator acts as a pointer to data within container classes but they are only correct up to the point you start adding or removing data.. this isn't good for me since i want to create a pointer to data and delete stuff from the list as well

So how can I get a pointer to the data in the container that will still work after I add/delete items from the container? Or will i just have to create a vector of pointers instead?

- Nick

Re:C++ STL Containers

Posted: Fri Oct 03, 2003 6:38 pm
by ark
in general, an iterator remains valid even when you're adding and deleting items, as long as you don't delete the item that the iterator is currently referencing.

in general, this is only a problem if you intend to go through your container class deleting multiple items using your iterator or if you have two iterators going at the same time and one deletes the item that the other one is currently referencing. In general, it should work just fine as long as you remember that the iterator is invalidated and has to be reset or reinstantiated or whatever after the item it is currently referencing has been deleted.

Re:C++ STL Containers

Posted: Fri Oct 03, 2003 7:45 pm
by sonneveld
How is a vector implemented? is it just like an array? so if I delete an item in the middle, will it have to shift all the next items down one? If this happens, won't half of my interators be immedially invalid?

- Nick

Re:C++ STL Containers

Posted: Fri Oct 03, 2003 7:56 pm
by sonneveld
Depends on the container.. I just realised.

If I use a list, instead of a vector, the other iterators will still be valid if I delete in the middle. (slaps forehead)

- Nick

Re:C++ STL Containers

Posted: Fri Oct 03, 2003 8:40 pm
by sonneveld
oohh.. one more thing.. whats the best way to get a pointer from an iterator? I can't cast unless I do this roundabout way of doing it:

Code: Select all

FoundGameVector::iterator i; // Iterator for looping over list elements
   for ( i = fndVect.begin(); i != fndVect.end(); ++i )
   {
      listy->Append(_T("blah"), (void*)&(*i));
   }
- Nick

Re:C++ STL Containers

Posted: Fri Oct 03, 2003 10:14 pm
by ark
unless you go with the option of storing pointers in the container itself, I'm not sure there is any other option. Even then, it would appear you'd only be getting rid of the address-of operator in there.

I will say don't take my word for it, though. Honestly, I wrote my own linked list and iterator template classes that I use, since I often times find the STL's nearly excessive operator overloading to be almost counter-intuitive rather than helpful. I know only the bare minimum about the STL.