Quick Q: Does unordered_map<string,MyClass>::erase() destroy my MyClass objects? -- SO

Quick A: No, but unordered_map<string, unique_ptr<MyClass>>::erase and unordered_map<string, shared_ptr<MyClass>>::erase do.

Today on SO:

std::unordered_map<std::String, myClass*> -- does std::unordered_map::erase() call myClass' DTor?

Assume I have some unordered_map of pointers to class instances, would erasing an object from that map also delete the instance?

(rewording the question:) If I wanted to delete that instance, which version would be right?

if(it != map.end())
{
    delete it->second;
    map.erase(it);
}

or simply

if(it != map.end())
    map.erase(it);

?

Add a Comment

Comments are closed.

Comments (1)

1 0

Arthur O'Dwyer said on Oct 21, 2014 06:42 PM:

The title of the post refers to
unordered_map<string,MyClass>::erase()
, but of course you meant
unordered_map<string,MyClass*>::erase()
. (The StackOverflow question gets the wording correct.)