STL中map/vector的删除元素操作
在我们使用C++中的STL的时候,可以使用迭代器iterator进行遍历,但是当我们通过iterator对vector和map删除元素的时候,要格外的小心,往往操作不当,导致iterator失效,后果就是程序奔溃。
1. 对于vector,erase会返回下一个iterator。所以一般采用的方法是:
因为在使用erase的时候,删除元素前面的iterator有效,但是后面的iterator就不可预知了。
- #include <iostream>
- #include <vector>
- #include <iterator>
- using namespace std;
- void displayAllDate(vector<int>& inputDate)
- {
- vector<int>::iterator itor = inputDate.begin();
- cout << endl;
- while (itor != inputDate.end())
- {
- cout << *itor << " ";
- ++itor;
- }
- }
- int main()
- {
- vector<int> inputDate;
- for (int i = 0; i < 10; i++)
- {
- inputDate.push_back(i);
- }
- vector<int>::iterator iter = inputDate.begin();
- while (iter != inputDate.end())
- {
- if (0 == (*iter)%2)
- {
- iter = inputDate.erase(iter);
- }
- else
- {
- ++iter;
- }
- }
- displayAllDate(inputDate);
- return 0;
- }
运行结果:
2. 对于map,删除erase之后,只会影响到当前的iterator,因此我们一般推荐以下方法:
- #include <iostream>
- #include <map>
- #include <iterator>
- using namespace std;
- void displayAllDate(map<int, int>& inputDate)
- {
- map<int, int>::iterator itor = inputDate.begin();
- cout << endl;
- while (itor != inputDate.end())
- {
- cout << itor->second << " ";
- ++itor;
- }
- }
- int main()
- {
- map<int, int> inputDate;
- for (int i = 0; i < 10; i++)
- {
- inputDate[i] = i + 10;
- }
- map<int, int>::iterator iter = inputDate.begin();
- while (iter != inputDate.end())
- {
- if (0 == (iter->second) % 2)
- {
- inputDate.erase(iter++);
- }
- else
- {
- ++iter;
- }
- }
- displayAllDate(inputDate);
- return 0;
- }
运行结果:
但是要注意的一点,map的erase操作在window下面也可以用vector的方法实现,但是在linux下是编译不过的。所以推荐使用上面的方法。
- #include <iostream>
- #include <map>
- #include <iterator>
- using namespace std;
- void displayAllDate(map<int, int>& inputDate)
- {
- map<int, int>::iterator itor = inputDate.begin();
- cout << endl;
- while (itor != inputDate.end())
- {
- cout << itor->second << " ";
- ++itor;
- }
- }
- int main()
- {
- map<int, int> inputDate;
- for (int i = 0; i < 10; i++)
- {
- inputDate[i] = i + 10;
- }
- map<int, int>::iterator iter = inputDate.begin();
- while (iter != inputDate.end())
- {
- if (0 == (iter->second) % 2)
- {
- iter = inputDate.erase(iter);
- }
- else
- {
- ++iter;
- }
- }
- displayAllDate(inputDate);
- return 0;
- }
运行结果是:
参考:
http://www.cnblogs.com/dabaopku/p/3912662.html
STL中map/vector的删除元素操作相关推荐
- js中数组插入、删除元素操作
/** 删除数组元素:Array.removeArr(index)*/Array.prototype.removeArr = function (index) {if (isNaN(index) || ...
- C++中反向遍历map时怎样删除元素
文章目录 前言 map的正向遍历 map 遍历时删除元素 map 的反向遍历 map 反向遍历时删除元素 总结 前言 今天在解决一个问题 <5710. 积压订单中的订单总数> 时用到了ma ...
- C++ STL : 模拟实现STL中的vector类
文章目录 vector vector的介绍 vector的优缺点 实现时需要注意的细节问题 1. Capacity增长问题 2. memset等函数来带的按字节拷贝问题 3. 深浅拷贝问题 4. 迭代 ...
- C++ STL中Map的按Key排序和按Value排序
map是用来存放<key, value>键值对的数据结构,可以很方便快速的根据key查到相应的value.假如存储学生和其成绩(假定不存在重名,当然可以对重名加以区分),我们用map来进行 ...
- c++ map是有序还是无序的_C++ STL中Map的按Key排序和按Value排序
map是用来存放键值对的数据结构,可以很方便快速的根据key查到相应的value.假如存储学生和其成绩(假定不存在重名,当然可以对重名加以区分),我们用map来进行存储就是个不错的选择. 我们这样定义 ...
- STL 中map的用法详解
STL 中map的用法详解 Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可 ...
- stl中map函数_map :: max_size()函数,以及C ++ STL中的Example
stl中map函数 C ++ STL映射:: max_size() (C++ STL map::max_size() ) It returns the maximum number of elemen ...
- stl中map函数_map :: empty()函数以及C ++ STL中的Example
stl中map函数 C ++ STL映射:: empty() (C++ STL map::empty()) It is built-in function in C++ STL and used to ...
- STL中map和string, vector 用法详解
1. map 用法详解 std map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成 ...
最新文章
- 管道:介绍和基本服务
- 【高效JDBC编程工具JadePool快速入门】
- 【spring源码分析】IOC容器初始化(二)
- 如何用python批量下载数据_使用Python批量下载数据
- ThinkPHP模型连接数据库 查询 ajax
- 像拍电影一样做软件(译者序)原创
- C++优先队列priority_queue详解
- oracle函数lad,01-查询Oracle中所有用户信息
- mysql 主键溢出检查_详解MySQL 表中非主键列溢出情况监控
- 20164317《网络对抗技术》Exp3 免杀原理与实践
- mysql8.0数据回滚_大企业数据库服务首选!AliSQL这几大企业级功能你不可不知
- 皕杰报表使用技巧:竖排文字如何输入
- 我看过的关于职业规划最好最全面的一篇文章
- 银行数据仓库体系实践(15)--数据应用之巴塞尔新资本协议
- 面试问遇到最难的事情_太难的事情
- html里面的view怎么修改,asp.net mvc 3-在局部视图中修改MVC 3 ViewBag不会保留到_Layout.cshtml...
- Quartus II 13.1的安装与注册
- Java正则表达式的语法与示例
- 2021年企业服务行业BP和融资计划书PPT模板
- 因该如何搭建自己的网校系统呢?
热门文章
- 开发过程真相...太真实了!一毛一样有没有!
- 计算机教育中缺失的一课 · the missing semester of your cs education
- 想让好友不停地擦手机屏幕?微信头像这样设置就行了!
- 彻底理解 Redis 的持久化和主从复制
- 你应该使用Java8 非阻塞异步API来优化你的系统了
- Spring Boot 2.x基础教程:JdbcTemplate的多数据源配置
- Spring Cloud Stream消费失败后的处理策略(四):重新入队(RabbitMQ)
- Spring Cloud构建微服务架构:分布式配置中心(加密解密)
- python曲线拟合笔记
- module 'yaml' has no attribute 'FullLoader'