I have a map declared as
std::map< std::string, std::map<int,std::list<pointers*> > myMap; // in c++.
May I know in order to delete an entry in this map, is it sufficient by calling myMap.erase(entryToBeDeleted)? Or should I go and iterate through the sub-map to delete everything first and then delete the entry?
To add on, the inner map is a map with int as key and a list of pointers as value
It depends on the real type of the nested map, if it contains raw pointer, it’s your responsibilty to iterate though each item of nested map to deallocate the memory. Otherwise,
myMap.erase(entryToBeDeleted)is sufficient.Edit:
As your nested map has list of pointers, you need to go through each list inside each nested map to delete pointers.
A better way is to use smart pointers:
When you delete nested map, the pointers will be deallocated automatically.