|
-
March 28th, 2004, 03:56 AM
#1
delete an item in a hash_map map etc
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?
-
March 28th, 2004, 12:08 PM
#2
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.
-
March 29th, 2004, 03:06 AM
#3
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|