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容器中指针的释放
C++ STL map容器值为指针时怎么释放内存的更多相关文章
- c++继承构造子类调用父类构造函数的问题及关于容器指针的问题及当容器里储存指针时,记得要手动释放
看下面的一个问题: class Person { private: string name; public: Person(const string& s=""){ nam ...
- 详解C++ STL map 容器
详解C++ STL map 容器 本篇随笔简单讲解一下\(C++STL\)中的\(map\)容器的使用方法和使用技巧. map容器的概念 \(map\)的英语释义是"地图",但\( ...
- STL --> map容器
map容器 一.map简介 map是一类关联式容器.它的特点是增加和删除节点对迭代器的影响很小,除了那个操作节点,对其他的节点都没有什么影响.对于迭代器来说,可以修改实值,而不能修改key. 二.ma ...
- c++ STL map容器成员函数
map容器用于查找,设置键值和元素值,输入键值,就能得到元素值.map对象中的元素时刻都是有序的,除非无序插入的.它是用平衡树创建的.查找很快. 函数 描述,注意有r的地方都是不能用it代替的. ma ...
- STL关联容器值hashtable
hashtable(散列表)是一种数据结构,在元素的插入,删除,搜索操作上具有常数平均时间复杂度O(1); hashtable名词 散列函数:负责将某一元素映射为索引. 碰撞(collision):不 ...
- stl map容器 学习
#include<map> 1.map的声明: map<string,int>map_1; map_1 就是一个string对int的映射. 2.map的用法(映射): map ...
- C++ STL map容器的说明测试1
// maptest.cpp : 定义控制台应用程序的入口点.// #include "stdafx.h" /*********************************** ...
- map的erase()释放内存
STL中的map调用erase(it),当value值为指针时,释放内存: #include <iostream> #include <map> #include <st ...
- BestCoder Round #92 1001 Skip the Class —— 字典树 or map容器
题目链接:http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=748&pid=1001 题解: 1.trie树 关 ...
随机推荐
- 问题 F: 超超的自闭意思
问题 F: 超超的自闭意思 时间限制: 1 Sec 内存限制: 128 MB提交: 80 解决: 10[提交] [状态] [命题人:jsu_admin] 题目描述 质数定义为在大于1的自然数中,除 ...
- ssm框架搭建整合测试
下载各种jar包 mybatis下载 https://github.com/mybatis/mybatis-3/releases mysql驱动下载 http://mvnrepository.com/ ...
- 图像函数 imagecreatetruecolor()和imagecreate()的异同点
共同点:这两个函数都是用于创建画布 区别: 1.不同的是创建画布和为画布填充颜色的流程不一样; 用imagecreatetruecolor(int x,int y)创建的是一幅大小为 x和 y的图像( ...
- RabbitMQ走过的坑,发送的消息是乱码
发送的消息在可视化界面中是乱码,如图: 看见这个content_tpye没有,是不是很奇怪,就是这个坑,设置下就行,看代码: @Bean Jackson2JsonMessageConverter me ...
- LCT的一些坑【已经变成坑点集合了233】
好了蠢蠢的我写了第一个LCT模板就炸掉了QAQ 开个blog记一下我能出多少锅. 1.splay写挂了hhh这个你真的是蠢 2.这个奇怪的东西 bool not_root(int x){return ...
- 【leetcode】870. Advantage Shuffle
题目如下: 解题思路:几千年前,一个古人曾经解过这个题目,他的名字叫做田忌,后人称他的解题思想叫做“田忌赛马”.言归正传,本题就是一个田忌赛马的问题,先将A与B进行排序,然后判断A[0]与B[0]的大 ...
- python的可视化展示(待更新)
参考:https://www.cnblogs.com/zhizhan/p/5615947.html 通过matplotlib进行绘图: 例1: import matplotlib.pyplot as ...
- js获取(URL)地址栏参数
//获取地址栏参数 //url为空时为调用当前url地址 //调用方法为 var params = getPatams(); function getParams(url) { var theRe ...
- C#语句,console,C#//,/**/
].
- 无法启用web调试服务器
场景:vs2005 webservice 项目属性中:web : 使用IIS web 服务器 http://localhost/WSMA --- >自动创建虚拟目录 点击调试的时候F ...