C++ STL map容器值为指针时怎么释放内存

最近在使用STL中map时,遇到了一个问题,就是当map中值为指针对象时怎么释放内存?

// 站点与TCP连接映射表 (key为ip_port_stationCode, value为 clientSocket*)
std::map<String, DataUpload*> g_TcpConnMap;

// 遍历站点列表,为每个服务器id[ip:port]的每个站点(station code)建立一个TCP连接
for (auto& staionItem : server.Host().stationListConf)
{
 		// ip_port_stationCode 服务器地址_端口_站点编码 唯一确定一个TCP连接
 		char strTcpCode[128] = { 0 };
 		sprintf(strTcpCode, "%s_%d_%s", host.host, host.port, staionItem.sscode);
 		
 		String strTcpTemp(strTcpCode);

 		clientSocket* pclientSock=
 			new clientSocket(host.host, host.port);
 		
 		g_TcpConnMap.insert(std::make_pair(strTcpTemp, pclientSock));
}

// 释放资源
for (auto iter = g_TcpConnMap.begin(); iter != g_TcpConnMap.end(); )
{
 	auto second = iter->second;
 	if (second)
 	{
 		delete second;				  // 释放指针
 		second = NULL;
 		g_TcpConnMap.erase(iter++);	  // 从map中删除元素,注意iter++的写法
 	}
}

g_TcpConnMap.clear();

在std::list中删除一个元素非常简单,直接使用erase方法即可,代码如下:

for(iter = list.begin(); iter != list.end();) {
if (shouldDelete(*iter))
	iter = list.erase(iter);
else
	++iter;
}

或者更简单点

list.erase(std::remove_if(list.begin(), list.end(), shouldDelete), list_end());

然而根据STL std::map中的定义void erase(iterator pos),此erase并不返回下一个元素的迭代器,因此不能采用std::list的方法
The truth is that ‘erase’ in associative containers doesn’t invalidate any iterators except those that point to elements being erased (that’s also true for ’sid::list’). For this reason, you don’t really need ‘map::erase’ to return an iterator. Just do this

for(iter = map.begin(); iter != map.end();) {
  if (shouldDelete(*iter))
     map.erase(iter++);
  else
     ++iter;
}

当然此方法同样也适合于std::list等。

参考资料:
C++遍历中删除std::map元素
C++ map(STL map)删除元素(erase函数删除元素)详解
stl map容器中指针的释放

原文地址:https://www.cnblogs.com/ccf19881030/p/12004816.html