正确使用STL-MAP中Erase函数
一切尽在代码中。

#include <iostream>
#include <map>
#include <string>
using namespace std ; int main(void)
{
map<int, string> m ;
m.insert(pair<int, string>(1, "abc")) ;
m.insert(pair<int, string>(2, "def")) ;
m.insert(pair<int, string>(3, "def")) ;
m.insert(pair<int, string>(4, "ghi")) ; map<int, string>::iterator itor ; // 错误的写法
for (itor = m.begin(); itor != m.end(); ++itor)
{
if (itor->second == "def")
{
m.erase(itor) ; // map是关联式容器,调用erase后,当前迭代器已经失效
}
} // 正确的写法
for (itor = m.begin(); itor != m.end();)
{
if (itor->second == "def")
{
m.erase(itor++) ; // erase之后,令当前迭代器指向其后继。
}
else
{
++itor;
}
} // 另一个正确的写法,利用erase的返回值,注意,有些版本的stl-map没有返回值,比如SGI版,但vc版的有
for (itor = m.begin(); itor != m.end();)
{
if (itor->second == "def")
{
itor = m.erase(itor) ; // erase的返回值是指向被删除元素的后继元素的迭代器
}
else
{
++itor;
}
} // Print m
map<int, string>::const_iterator citor ;
for (citor = m.begin(); citor != m.end(); ++citor)
{
cout << citor->first << ":" << citor->second << endl ;
} getchar() ;
return 0 ;
}

正确使用STL-MAP中Erase函数的更多相关文章
- 正确使用stl map的erase方法
先声明:下面的文章是针对windows的用法,因为std::map的erase函数的windows的实现版本是返回一个std::map的迭代器,但是STL标准里面的该函数的返回值确是: map.era ...
- stl::map之const函数访问
如何在const成员数中访问stl::map呢?例如如下代码: string ConfigFileManager::MapQueryItem(const string& name) const ...
- [转载]STL map中的一些基本函数
来源:(http://blog.sina.com.cn/s/blog_61533c9b0100fa7w.html) - C++ map的基本操作和使用_Live_新浪博客 Map是c++的一个标准容器 ...
- c++ STL map容器成员函数
map容器用于查找,设置键值和元素值,输入键值,就能得到元素值.map对象中的元素时刻都是有序的,除非无序插入的.它是用平衡树创建的.查找很快. 函数 描述,注意有r的地方都是不能用it代替的. ma ...
- stl map中的lower_bound和 upper_bound
map中的lower_bound和upper_bound的意思其实很简单,就两句话: map::lower_bound(key):返回map中第一个大于或等于key的迭代器指针 map::upper_ ...
- STL库中神奇函数nth_element
用法:nth_element(数组名,数组名+第k小元素,数组名+元素个数) 这个函数主要用来将数组元素中第k小的整数排出来并在数组中就位,随时调用. 例如: ]={,,,,},k ; cin> ...
- map 和 vector 的erase函数说明
1. map的erase函数使用 这里首先要注意,C++针对map的erase函数有不同的函数原型,这往往是出现问题的关键所在.根据参考文献1: 在C++98中: (1) void erase (it ...
- map中使用await 异步函数
let result=await Promise.all(dataComments.map(async (ele)=>{ return (async ()=>{ let resData= ...
- 浅析 JavaScript 中的 函数 currying 柯里化
原文:浅析 JavaScript 中的 函数 currying 柯里化 何为Curry化/柯里化? curry化来源与数学家 Haskell Curry的名字 (编程语言 Haskell也是以他的名字 ...
随机推荐
- sql查询统计,根据新闻类别ID统计,没有数据显示0
有两张表,新闻信息表MessageInfo和新闻类别表MessageType.表结构如下: 然后需要实现下面这种查询结果: 这个是我面试时遇到的,上面的新闻类型是乱写的.当时没有做出来,然后回来又研究 ...
- Contoso 大学 - 4 - 创建更加复杂的数据模型
原文 Contoso 大学 - 4 - 创建更加复杂的数据模型 原文地址:http://www.asp.net/mvc/tutorials/getting-started-with-ef-using- ...
- Swift调用Objective-C
Swift调用Objective-C需要一个名为“<工程名>-Bridging-Header.h”的桥接头文件,如下图所示.桥接头文件的作用是为Swift调用Objective-C对象搭建 ...
- button上加上图片的两种方式
//// ViewController.m// UIButtonDemo//// Created by hehe on 15/9/15.// Copyright (c) 2015年 wang. ...
- 如何让Eclipse的智能提示像VS一样霸气
说起来用 Eclipse 也有一段时间了,相信每一个用过的人都知道他的智能提示功能真的是糟糕透了,与 VisualStudio2008 简直不是一个档次的!我就纳闷了,他为什么不弄好一点呢!今天我实在 ...
- browserify.js 的模块加载
browserify的模块加载基本上和nodejs的类似: nodejs 的模块加载是依次去读取文件然后用一个类eval() 函数执行并返回module.exports的结果.为了避免循环加载,在加载 ...
- Remote Desktop Organizer远程桌面管理软件的基本使用和介绍
<Remote Desktop Organizer>是一款用于远程桌面管理的软件.软件支持windows平台运行. Remote Desktop Organizer 是一款 Windows ...
- poj 2154 Color
这是道标准的数论优化的polya题.卡时卡的很紧,需要用int才能过.程序中一定要注意控制不爆int!!!我因为爆intWA了好久=_=…… 题目简洁明了,就是求 sigma n^gcd(i,n):但 ...
- Poj OpenJudge 1068 Parencodings
1.Link: http://poj.org/problem?id=1068 http://bailian.openjudge.cn/practice/1068 2.Content: Parencod ...
- Layout.xml中控件的ID命名方式
控件 缩写 LayoutView lv RelativeView rv TextView tv Button btn ImageButton imgBtn ImageView mgView 或则 iv ...