Archived

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

Impz0r

How to delete an vector<> cell correct?!

Recommended Posts

Hi, I''ve problems with the std::vector class! I would delete some cells of the vector, but this gimme an runtime error. My code is like these snipped: vector::iterator i = m_aGameServer.begin (); vector::iterator End = m_aGameServer.end (); while (i != End) { if ((+i)->bFavorite) // dont delete else { // delete this shit delete (*i); m_aGameServer.erase (i); // this wont work End = m_aGameServer.end (); // same here, wont work } ++i; } So the vector must hold all Game Servers that are favorites, and the others must be erase from these list. Hope you understand the problem Mfg Impz0r

Share this post


Link to post
Share on other sites
I''m not sure what data type is in the vector, but you have one common problem regardless of the data type...

Iterators are invalidated after the mutation of a sequence.

The erase() call means your iterators are now invalid so you should reset them.

Or you should use the remove_if() generic algorithm, iterate over that iters of the returned removal elements and then erase them.

Once you call erase() you need to start from the beginning again, that is why there is the remove_if() algorithm which just sorts the data structure and returns you an iterator to the first element that matches the condition.

Also, if you do this a lot you may want to consider a deque or a list because erasing in the middle of a vector is inefficient - it will copy ( or assign? ) all the elements to a new vector so that all the elements are contiguous. If the vector is large and the types complex it could be quite time consuming.

Share this post


Link to post
Share on other sites
hi,

Thanks for your answer, but my compiler sys me he can''t found these remove_if() function..wtf?! I''ve search for it in my lib''s but i don''t found it at all, did i have the wrong, or old std lib version, or what''s going on here?!

Mfg Impz0r

Share this post


Link to post
Share on other sites
I cannot see why End = m_aGameServer.end(); fails, however you are invalidating your itterator i with the call to erase(). change that to i = erase(i); You should also be using a list if you want to delete elements "in place". With a vector of pointers you have another option of simply deleting and then setting the pointer to NULL. This requires a test everywhere that you retreve the pointer but it is generally a good call.

the stl algorithm remove_if might be more what you are loking for

struct not_favorite {
bool operator ()(ServerType *s) {return s->bFavorite);
};

template
struct deletor {
void operator()(Type *p) {delete p;}
};

ittr new_end=remove_if(m_aGameServer.begin(), m_aGameServer.end(), not_favorite());
for_each(new_end,m_aGameServer.end(),deletor());
m_aGameServer.erase(new_end,m_aGameServer.end());


If you are feeling lucky, you can change not_favorite to

struct not_favorite {
bool operator()(ServerType *s) {
if(!s->bFavorite) {
delete s;
return true;
} else {
return false;
}
}
};
and skip the for_each, this is a bit shaky though.

Share this post


Link to post
Share on other sites
Here you go:

    
vector<TGameServer *>::iterator itr = m_aGameServer.begin();
vector<TGameServer *>::iterator stop = m_aGameServer.end();
//

while(itr != stop)
{
if(!itr->bFavorite) // iterators are pointers

itr = m_aGameServer.erase(itr); // erase returns the next valid pointer

else
++itr;
}


[Edit:] Typo.

Edited by - Oluseyi on November 16, 2001 4:35:46 PM

Share this post


Link to post
Share on other sites
quote:

Thanks for your answer, but my compiler sys me he can't found these remove_if() function


did you:
  
#include <algorithm>



Edited by - invective on November 17, 2001 5:10:24 AM

Share this post


Link to post
Share on other sites