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树 关 ...
随机推荐
- LED音乐频谱之点阵
转载请注明出处:http://blog.csdn.net/ruoyunliufeng/article/details/37967455 一.硬件 watermark/2/text/aHR0cDovL2 ...
- mongoDB学习笔记(2)
一.删数据库 1.语法 MongoDB 删除数据库的语法格式如下: db.dropDatabase() 删除当前数据库,默认为 test,你可以使用 db 命令查看当前数据库名. 2.实例 以下实例我 ...
- 2019-3-6-WPF-使用-SharpDX
title author date CreateTime categories WPF 使用 SharpDX lindexi 2019-03-06 16:52:37 +0800 2018-4-20 9 ...
- MacBook Pro修改hosts
访达前往:/etc/hosts 将hosts复制到桌面修改保存 替换 附Windows hosts文件位置: C:\windows\System32\drivers\etc
- linux--基础知识5
#文件合并与文件归档 #cat /etc/passwd > new_pass.txt (创建一个新的文档并将cat/etc/passwd的内容合并进来) #echo "xxxx&quo ...
- python的type和object
在python中一切皆对象,这是个用python的人都知道的概念,以int举例,比如a=2,type下: 发现他的type是int,在python中type就是类,所以a是类int的一个对象,实例是类 ...
- opencv windows源码编译
WITH_QT//H:\software\programming\qt\5.12.3\mingw73_32\lib\cmake 5.6的路径要改这样 WITH_OPENGL 编译器mingw32-m ...
- 浅谈ContextLoaderListener及其上下文与DispatcherServlet的区别
一般在使用SpingMVC开发的项目中,一般都会在web.xml文件中配置ContextLoaderListener监听器,如下: <listener> <listener-clas ...
- loj6038「雅礼集训 2017 Day5」远行 树的直径+并查集+LCT
题目传送门 https://loj.ac/problem/6038 题解 根据树的直径的两个性质: 距离树上一个点最远的点一定是任意一条直径的一个端点. 两个联通块的并的直径是各自的联通块的两条直径的 ...
- Centos7.5中的SElinux操作命令说明
设置Selinux模式 setenforce 0 0表示警告模式 1表示强制模式 关闭要设置/etc/sysconfig/selinux下将"SELINUX=enforcing"改 ...