在我们使用C++中的STL的时候,可以使用迭代器iterator进行遍历,但是当我们通过iterator对vector和map删除元素的时候,要格外的小心,往往操作不当,导致iterator失效,后果就是程序奔溃。

1. 对于vector,erase会返回下一个iterator。所以一般采用的方法是:

因为在使用erase的时候,删除元素前面的iterator有效,但是后面的iterator就不可预知了。

[cpp] view plaincopy
  1. #include <iostream>
  2. #include <vector>
  3. #include <iterator>
  4. using namespace std;
  5. void displayAllDate(vector<int>& inputDate)
  6. {
  7. vector<int>::iterator itor = inputDate.begin();
  8. cout << endl;
  9. while (itor != inputDate.end())
  10. {
  11. cout << *itor << " ";
  12. ++itor;
  13. }
  14. }
  15. int main()
  16. {
  17. vector<int> inputDate;
  18. for (int i = 0; i < 10; i++)
  19. {
  20. inputDate.push_back(i);
  21. }
  22. vector<int>::iterator iter = inputDate.begin();
  23. while (iter != inputDate.end())
  24. {
  25. if (0 == (*iter)%2)
  26. {
  27. iter = inputDate.erase(iter);
  28. }
  29. else
  30. {
  31. ++iter;
  32. }
  33. }
  34. displayAllDate(inputDate);
  35. return 0;
  36. }

运行结果:

2. 对于map,删除erase之后,只会影响到当前的iterator,因此我们一般推荐以下方法:

[cpp] view plaincopy
  1. #include <iostream>
  2. #include <map>
  3. #include <iterator>
  4. using namespace std;
  5. void displayAllDate(map<int, int>& inputDate)
  6. {
  7. map<int, int>::iterator itor = inputDate.begin();
  8. cout << endl;
  9. while (itor != inputDate.end())
  10. {
  11. cout << itor->second << " ";
  12. ++itor;
  13. }
  14. }
  15. int main()
  16. {
  17. map<int, int> inputDate;
  18. for (int i = 0; i < 10; i++)
  19. {
  20. inputDate[i] = i + 10;
  21. }
  22. map<int, int>::iterator iter = inputDate.begin();
  23. while (iter != inputDate.end())
  24. {
  25. if (0 == (iter->second) % 2)
  26. {
  27. inputDate.erase(iter++);
  28. }
  29. else
  30. {
  31. ++iter;
  32. }
  33. }
  34. displayAllDate(inputDate);
  35. return 0;
  36. }

运行结果:

但是要注意的一点,map的erase操作在window下面也可以用vector的方法实现,但是在linux下是编译不过的。所以推荐使用上面的方法。

[cpp] view plaincopy
  1. #include <iostream>
  2. #include <map>
  3. #include <iterator>
  4. using namespace std;
  5. void displayAllDate(map<int, int>& inputDate)
  6. {
  7. map<int, int>::iterator itor = inputDate.begin();
  8. cout << endl;
  9. while (itor != inputDate.end())
  10. {
  11. cout << itor->second << " ";
  12. ++itor;
  13. }
  14. }
  15. int main()
  16. {
  17. map<int, int> inputDate;
  18. for (int i = 0; i < 10; i++)
  19. {
  20. inputDate[i] = i + 10;
  21. }
  22. map<int, int>::iterator iter = inputDate.begin();
  23. while (iter != inputDate.end())
  24. {
  25. if (0 == (iter->second) % 2)
  26. {
  27. iter = inputDate.erase(iter);
  28. }
  29. else
  30. {
  31. ++iter;
  32. }
  33. }
  34. displayAllDate(inputDate);
  35. return 0;
  36. }

运行结果是:

参考:

http://www.cnblogs.com/dabaopku/p/3912662.html

STL中map/vector的删除元素操作相关推荐

  1. js中数组插入、删除元素操作

    /** 删除数组元素:Array.removeArr(index)*/Array.prototype.removeArr = function (index) {if (isNaN(index) || ...

  2. C++中反向遍历map时怎样删除元素

    文章目录 前言 map的正向遍历 map 遍历时删除元素 map 的反向遍历 map 反向遍历时删除元素 总结 前言 今天在解决一个问题 <5710. 积压订单中的订单总数> 时用到了ma ...

  3. C++ STL : 模拟实现STL中的vector类

    文章目录 vector vector的介绍 vector的优缺点 实现时需要注意的细节问题 1. Capacity增长问题 2. memset等函数来带的按字节拷贝问题 3. 深浅拷贝问题 4. 迭代 ...

  4. C++ STL中Map的按Key排序和按Value排序

    map是用来存放<key, value>键值对的数据结构,可以很方便快速的根据key查到相应的value.假如存储学生和其成绩(假定不存在重名,当然可以对重名加以区分),我们用map来进行 ...

  5. c++ map是有序还是无序的_C++ STL中Map的按Key排序和按Value排序

    map是用来存放键值对的数据结构,可以很方便快速的根据key查到相应的value.假如存储学生和其成绩(假定不存在重名,当然可以对重名加以区分),我们用map来进行存储就是个不错的选择. 我们这样定义 ...

  6. STL 中map的用法详解

    STL 中map的用法详解 Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可 ...

  7. 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 ...

  8. 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 ...

  9. STL中map和string, vector 用法详解

    1. map 用法详解 std map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成 ...

最新文章

  1. 管道:介绍和基本服务
  2. 【高效JDBC编程工具JadePool快速入门】
  3. 【spring源码分析】IOC容器初始化(二)
  4. 如何用python批量下载数据_使用Python批量下载数据
  5. ThinkPHP模型连接数据库 查询 ajax
  6. 像拍电影一样做软件(译者序)原创
  7. C++优先队列priority_queue详解
  8. oracle函数lad,01-查询Oracle中所有用户信息
  9. mysql 主键溢出检查_详解MySQL 表中非主键列溢出情况监控
  10. 20164317《网络对抗技术》Exp3 免杀原理与实践
  11. mysql8.0数据回滚_大企业数据库服务首选!AliSQL这几大企业级功能你不可不知
  12. 皕杰报表使用技巧:竖排文字如何输入
  13. 我看过的关于职业规划最好最全面的一篇文章
  14. 银行数据仓库体系实践(15)--数据应用之巴塞尔新资本协议
  15. 面试问遇到最难的事情_太难的事情
  16. html里面的view怎么修改,asp.net mvc 3-在局部视图中修改MVC 3 ViewBag不会保留到_Layout.cshtml...
  17. Quartus II 13.1的安装与注册
  18. Java正则表达式的语法与示例
  19. 2021年企业服务行业BP和融资计划书PPT模板
  20. 因该如何搭建自己的网校系统呢?

热门文章

  1. 开发过程真相...太真实了!一毛一样有没有!
  2. 计算机教育中缺失的一课 · the missing semester of your cs education
  3. 想让好友不停地擦手机屏幕?微信头像这样设置就行了!
  4. 彻底理解 Redis 的持久化和主从复制
  5. 你应该使用Java8 非阻塞异步API来优化你的系统了
  6. Spring Boot 2.x基础教程:JdbcTemplate的多数据源配置
  7. Spring Cloud Stream消费失败后的处理策略(四):重新入队(RabbitMQ)
  8. Spring Cloud构建微服务架构:分布式配置中心(加密解密)
  9. python曲线拟合笔记
  10. module 'yaml' has no attribute 'FullLoader'