C++ std::map::erase用法及其陷阱
1.引入:
STL的map中有一个erase方法用来从一个map中删除制定的节点
eg:
map<string,string> mapTest;
typedef map<string,string>::iterator ITER;
ITER iter=mapTest.find(key);
mapTest.erase(iter);
像上面这种删除单个节点,map的行为不会出现问题,但是当在一个循环里用的时候,往往会被误用。
2.陷阱
eg:
for(ITER iter=mapTest.begin();iter!=mapTest.end();++iter)
{
cout<<iter->first<<":"<<iter->second<<endl;
mapTest.erase(iter);
}
这是一种错误的写法,会导致程序行为不可知,原因是map是关联容器,对于关联容器来说,如果一个元素已经被删除,那么其对应的迭代器就失效了,不应该再被使用,负责会导致程序无定义的行为。
3.正确用法
正确用法一:(C++11可以,C++98不行)
erase() 成员函数返回下一个元素的迭代器
std::map<std::string, std::string >::iterator it = mapTest.begin();
while(it != mapTest.end())
{
if(TestVal(it->second))
{
it = mapTest.erase(it);
}
else
it++;
}
正确用法二:
使用删除之前的迭代器定位下一个元素。STL建议的使用方式
for(ITER iter=mapTest.begin();iter!=mapTest.end();) //注意此处不能再写iter++
{
cout<<iter->first<<":"<<iter->second<<endl;
mapTest.erase(iter++);
}
在这种用法中,该方法中利用了后++的特点,这个时候执行mapTest.erase(it++);这条语句分为三个过程
1、先把it的值赋值给一个临时变量做为传递给erase的参数变量
2、因为参数处理优先于函数调用,所以接下来执行了it++操作,也就是it现在已经指向了下一个地址。
3、再调用erase函数,释放掉第一步中保存的要删除的it的值的临时变量所指的位置。
4.例子
case1: ✅
#include<map>
#include<iostream>
int main()
{
std::map<int, int> map_a;
std::map<int, int>::iterator it; for (int i = ; i != ; i++) {
map_a.insert(std::map<int, int>::value_type(i, i));
} for (it = map_a.begin(); it != map_a.end(); it++){
std::cout<< it->first <<std::endl;
// map_a.erase(it->first);
} return ;
}
结果:
case2:❌
#include<map>
#include<iostream>
int main()
{
std::map<int, int> map_a;
std::map<int, int>::iterator it; for (int i = ; i != ; i++) {
map_a.insert(std::map<int, int>::value_type(i, i));
} for (it = map_a.begin(); it != map_a.end(); it++){
std::cout<< it->first <<std::endl;
map_a.erase(it->first);
} return ;
}
结果:
当将map大小加到1000000的时候,出core
5.导致程序行为不可知的原因
5.1 list和vector的erase方法都返回一个iterator,指向被删除元素的下一个元素,唯独map,set,multimap,multiset这4个竟然返回void.
不过在C++11中已经改正过来了,都返回一个iterator

原因:迭代器++的操作是在操作它保存的节点指针,所以当这个节点被删除的时候,改节点所指向的指针是无法确定的,也就是可能还是以前的数据,也可能是已经其他毫不相关的数据了,这样的行为被叫做未定义的行为,不一定会core.
C++ std::map::erase用法及其陷阱的更多相关文章
- std::map用法
STL是标准C++系统的一组模板类,使用STL模板类最大的好处就是在各种C++编译器上都通用. 在STL模板类中,用于线性数据存储管理的类主要有vector, list, map 等等.本文主要 ...
- map::erase陷阱
map::erase函数在不同版本stl中的差异 1. C++98和C++11标准 http://www.cplusplus.com/reference/map/map/erase/ 2. pj st ...
- [转] C++ STL中map.erase(it++)用法原理解析
总结一下map::erase的正确用法. 首先看一下在循环中使用vector::erase时我习惯的用法: for(vector<int>::iterator it = vecInt.be ...
- C++ std::map用法简介
#include "map" //引入头文件 初始化: std::map <int, std::string> _map1; //初始化 //c++11中引入的,可以直 ...
- c++map的用法 分类: POJ 2015-06-19 18:36 11人阅读 评论(0) 收藏
c++map的用法 分类: 资料 2012-11-14 21:26 10573人阅读 评论(0) 收藏 举报 最全的c++map的用法 此文是复制来的0.0 1. map最基本的构造函数: map&l ...
- STL中map的用法
map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候 ...
- C++11中map的用法
最全的c++map的用法 1. map最基本的构造函数:map<string ,int>mapstring; map<int,string >mapint;map<sri ...
- 关于map::erase的使用说明
C++ 中经常使用的容器类有vector,list,map.其中vector和list的erase都是返回迭代器,但是map就比较不一样. 当在循环体中使用map::erase语句时,为了能够在任何机 ...
- C语言 · C++中map的用法详解
转载自:http://blog.csdn.net/sunquana/article/details/12576729 一.定义 (1) map<string, int> Map ...
随机推荐
- GDI+图形图像处理技术中Pen和Brush的简单使用和简单图形的绘制(C#)
1.Graphics Graphics对象是GDI+绘图表面,因此在Windows窗体应用程序中要使用GDI+创建绘图,必须要先创建Graphics.在给窗体注册一个Paint事件后,Graphics ...
- 提交ajax验证用户名是否已存在
前端页面 <tr> <td class="p_label"><span class="notnull"></span& ...
- iOS 8下使用xib/storybord AutoLayout导致的分割线问题
/*** iOS8 分割线问题 在xib/storyboard下面解决方案 http://qiita.com/yimajo/items/10f16629200f1beb7852 http://www. ...
- Asp.net操作cookie大全
实例代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 3 ...
- Windows系统下部署安装一个/多个Tomcat8
首先从http://tomcat.apache.org/上下载Tomcat8.0压缩版的,解压到指定路径后即可. 第一:在Windows系统中安装部署单个Tomcat 对于这种情况, ...
- 通过Guid获取DirectoryEntry对象
绑定DirectoryEntry对象通常有两种方法,一种是通过字符串(如通过ADsPath),一种是通过Guid(参见:http://msdn.microsoft.com/zh-cn/library/ ...
- windows下nginx的启动关闭
Windows下Nginx的启动.停止等命令 在Windows下使用Nginx,我们需要掌握一些基本的操作命令,比如:启动.停止Nginx服务,重新载入Nginx等,下面我就进行一些简单的介绍. .启 ...
- dhtmlx相关
主页:http://dhtmlx.com/ 文档地址:http://docs.dhtmlx.com/ 后台:https://dhtmlx.com/docs/products/dhtmlxConnect ...
- Ansible常用模块
http://liumissyou.blog.51cto.com/4828343/1749121
- JavaScript-取消事件-e.preventDefault();
取消事件:(阻止默认行为) 当事件执行过程中,遇到问题,可取消事件.不再触发 如何e.preventDefault(); <!DOCTYPE html> <html> < ...