CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2003
    Posts
    815

    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?

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    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.

  3. #3
    Join Date
    Sep 2003
    Posts
    815
    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
  •  





Click Here to Expand Forum to Full Width

Featured