Tuesday, 13 August 2013

C++ map destruction

C++ map destruction

I want to clear a map inside the destructor, but I do not know whether it
is empty or not. Is it okay if I clear it as follows.
for(std::map<int, int *>::iterator it = m_map.begin(); it != m_map.end();
++it)
{
delete it->second;
}
m_map.clear();
or should I also put an if condition around the above operation like
if(!m_map.empty()){} in order to check whether it is empty or not.

No comments:

Post a Comment