Click to See Complete Forum and Search --> : delete an item in a hash_map map etc


avi123
March 28th, 2004, 02:56 AM
Hi,

I have a std::hash_map

every item is dynamically allocated

and I want to delete an item in the hash map

so what I do is this:

/////////////////////////////////////////////////////////////////////

hash_map <int, myStruct*>::const_iterator it;

//search for the item to delete
it = m_MyTable.find(nKey);

if(it != m_MyTable.end()) //found
delete it->second;

////////////////////////////////////////////////////////////////////////
so I free the allocate item with the delete

my question is:

does my hash_map (m_MyTable) is know with 1 less entry
or that the enrty is not deleted and just its data part was deleted?

if this is the case, how do I delete the entry?

Thanks
avi

p.S should I use erase after the delete statment?

Philip Nicoletti
March 28th, 2004, 11:08 AM
I don't ave hash_map on this computer to verify, but I am
sure that you still need to do an erase(). You can verify:

1) look at the size() function ? same before and after ?

2) do another find() , does it still ind the entry in the hash_map ?

3) you are using a "const_iterator" .. this does not allow
ANY change in the hash_map. When you do the erase,
I would think you would need to use a normal iterator.

avi123
March 29th, 2004, 02:06 AM
yep, the const_interator was a mistake, I need to use a regullar one.

hash_map erase ,find & iterator are suppuse to be like map

Thanks
avi