Archived

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

Draco5869

What Exactly is an iterator and what does * do

Recommended Posts

What Exactly is an iterator and what does * do to it? Like what if i want to delete the pointers that it holds? I have _vSprites that holds all of the sprites in the game and then _vLineBlocks that holds all of the blocks in the game. I want to delete the sprites on a specific line. i would really appreciate this =). ----------------------------------------------------------- Like a sponge!

Share this post


Link to post
Share on other sites
I assmue that you are talking about C++ STL iterators? Iterators are used to access the elements of the container classes. They are basically an OOP wrapper for the pointer, and can, therefore, be used similarly. Just as with a pointer, dereferencing an iterator will return the object the iterator is associated with (points to). Each of the STL container types has an associated iterator, and there are different types of iterators as well. For a fuller definition, have a search on Google.
quote:

Like what if i want to delete the pointers that it holds? I have _vSprites that holds all of the sprites in the game and then _vLineBlocks that holds all of the blocks in the game. I want to delete the sprites on a specific line.


To delete the container elements, there is an erase() method, which takes an iterator to the element that you wish to delete. Maybe if I give you an example of erasing elements you can apply it to your situation:

std::list<std::string> str_list;

str_list.push_back("Hello, ");
str_list.push_back("Mr. ");
str_list.push_back("world!");

str_list.erase( str_list.begin() + 1 );

If when you say, "what if I want to delete the pointers that it holds?", you mean that you have a container of pointers, you should be careful. The erase() method merely deletes the container''s type element from the container. For example, with std::vector<CLine*> lines, lines.erase(iter) will delete the pointer-to-CLine pointed to by iter, not the actual object pointed to by iter. If you give more information, then we will be able to help more. As I said, have a look on Google for more information.

[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]

Share this post


Link to post
Share on other sites
Thanks for your explanation. What i ended up doing was:
delete (*siSprite) to delete the actual memory and then
m_vSprites.erase(siSprite) to delete it from the vector and it seems to work. thankies.

-----------------------------------------------------------
Like a sponge!

Share this post


Link to post
Share on other sites
Whoa! Just because it compiles doesn''t mean it works. When you use the delete operator you use it on a pointer. Putting the * symbol in front of a variable "dereferences" it, or lets you use it as if it were not a pointer. Using delete (*siSprite) will most likely take the member variables of siSprite and treat them as a memory address. I''m surprised this doesn''t crash. Just use delete siSprite and then erase.

Share this post


Link to post
Share on other sites
nope, if siSprites is an iterator to a list that stores pointers then *siSprites will return the value the iterator is pointing to, which is a pointer. so it can be safely deleted. the problem would occur if you try to delete the iterator itself


Share this post


Link to post
Share on other sites
quote:
Original post by Raloth
Whoa! Just because it compiles doesn't mean it works. When you use the delete operator you use it on a pointer. Putting the * symbol in front of a variable "dereferences" it, or lets you use it as if it were not a pointer. Using delete (*siSprite) will most likely take the member variables of siSprite and treat them as a memory address. I'm surprised this doesn't crash. Just use delete siSprite and then erase.

No, it's a vector of pointers, so "dereferencing" the iterator will return a pointer to a sprite:

std::vector<CSprite*> sprites;
sprites.push_back( new CSprite(...) );
sprites.push_back( new CSprite(...) );
// etc.

...

std::vector<CSprite*>::iterator iter = sprites.begin();   // iterator to pointer-to-CSprite
...
delete *iter;    // free the memory pointed to by the pointer object pointed to by the iterator ()
sprites.erase(iter);   // release the pointer-to-CSprite element in the vector

So, just to clarify, you are deleting correctly, Draco5869.

[edited by - Lektrix on July 9, 2003 11:38:09 AM]

Share this post


Link to post
Share on other sites