我有一张填满的 map ,现在我想彻底删除内存。我该怎么做?找不到与此主题相关的任何内容,抱歉,如果已经有人回答...
我的代码是这样的:
for(std::map<short,std::string>::iterator ii=map.begin();
ii!=map.end(); ++ii)
{
delete ⅈ
}
但它不起作用。有人可以帮忙吗?
问候, 菲尔
最佳答案
正确的做法是不去做。当 map
被自动分配的任何资源销毁时,它会自动释放资源。
除非您使用new
分配值,否则您不会删除
它们。
{
std::map<short,std::string> x;
x[0] = "str";
}
//no leaks here
{
std::map<short,std::string*> x;
x[0] = new std::string;
delete x[0];
}
关于c++ - 完全删除 std::map<int, string> 的内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15924488/