erase函数要么删作指定位置loc的元素,要么删除区间[start, end)的所有元素.

返回值是指向删除的最后一个元素的下一位置的迭代器

Parameters

All parameters are of member type iterator, which in vector containers are defined as a random access iterator type.

position
Iterator pointing to a single element to be removed from the vector.
first, last
Iterators specifying a range within the vector to be removed: [first,last). i.e., the range includes all the elements between first and last, including the element pointed by first but not the one pointed by last.

Return value

A random access iterator pointing to the new location of the element that followed the last element erased by the function call, which is the vector end if the operation erased the last element in the sequence.

删除值为3的元素,按说it = vec.erase(it); 是正确的,但是我实验发现 it = vec.erase(it);和 vec.erase(it);都可以work,没有crash。

  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. vector<int> vec;
  9. vec.push_back();
  10. vec.push_back();
  11. vec.push_back();
  12. vec.push_back();
  13. vec.push_back();
  14.  
  15. for(vector<int>::iterator it = vec.begin(); it != vec.end(); it++)
  16. {
  17. if(*it == )
  18. {
  19. it = vec.erase(it);
  20. //vec.erase(it);
  21. cout << *it << "CCC" << endl;
  22. it --;
  23. }
  24. else
  25. cout << *it << endl;
  26. }
  27. return ;
  28. }

//注意上面不能写成

/*
        for(vector<int>::iterator it=arr.begin(); it!=arr.end(); it ++)
        {
            if(* it == 8)
            {
                arr.erase(it); //在erase后,it失效,并不是指向vector的下一个元素,it成了一个“野指针”。

}
        }
      
    */

正确使用stl vecotr erase函数的更多相关文章

  1. STL的erase函数和lower_bound

    前提摘要: [1]一般我们的区间是左闭右开,如下面例子2. [2]erase函数谨慎使用. [3]map也是有序保存的. [erase] 1,删除字符串的首字母: string s="ecu ...

  2. 正确使用STL-MAP中Erase函数

    一切尽在代码中. #include <iostream> #include <map> #include <string> using namespace std ...

  3. STL中使用reverse_iterator时,如何正确使用erase函数

    假设有一个list容器,顺序存储了0-9一个10个整数.现在要使用reverse_iterator迭代器来查找值为8和5的元素,并且将这两个数删除.先来看以下的解决方法: #include <i ...

  4. 正确使用stl map的erase方法

    先声明:下面的文章是针对windows的用法,因为std::map的erase函数的windows的实现版本是返回一个std::map的迭代器,但是STL标准里面的该函数的返回值确是: map.era ...

  5. 【C/C++开发】STL erase()函数使用要小心

    http://blog.sina.com.cn/s/blog_67b6b720010114d3.html erase()函数的功能是用来删除容器中的元素 删除某个容器里的某个元素:c.erase(T) ...

  6. STL中erase的小心使用

    先看如下一道改错题: #include<iostream> #include<vector> using namespace std; void print(vector< ...

  7. STL区间成员函数及区间算法总结

    STL区间成员函数及区间算法总结 在这里总结下可替代循环的区间成员函数和区间算法: 相比单元素遍历操作,使用区间成员函数的优势在于: 1)更少的函数调用 2)更少的元素移动 3)更少的内存分配 在区间 ...

  8. c++ STL unique , unique_copy函数

    一.unique函数 类属性算法unique的作用是从输入序列中"删除"全部相邻的反复元素. 该算法删除相邻的反复元素.然后又一次排列输入范围内的元素,而且返回一个迭代器(容器的长 ...

  9. STL中erase()的陷阱

    最近在刷stl源码剖析这本书时,对于vector的erase()函数引起了我的注意 在删除单个元素时是这样定义的: iterator erase(iterator position){ !=end() ...

随机推荐

  1. 济南学习 Day1 T2 pm

    [问题描述]栈是一种强大的数据结构,它的一种特殊功能是对数组进行排序.例如,借助一个栈,依次将数组 1,3,2 按顺序入栈或出栈,可对其从大到小排序:1 入栈:3 入栈:3 出栈:2 入栈:2 出栈: ...

  2. linux kernel with param

    Linux kernel support pass param to kernel, this params can be assigned at load time by insmod or mod ...

  3. 安装Java EE失败,解决方案

    笔者安装Java EE(版本是java_ee_sdk-7-jdk7-windows-x64-ml.exe)时,遇到错误提示提示"Could not find the required ver ...

  4. 一款类似塔防类的保卫羊村游戏android源码

    一款类似塔防类的保卫羊村游戏源码,这个游戏很像我们平时玩的塔防游戏的,游戏的源码比较完整的,大家可以修改一下或者添加一些广告就可以上线到应用商店了,而且这个游戏目前已经上线国内的一些应用商店了,360 ...

  5. mysql数据库的简单操作

    首先进入mysql:mysql -u root -p 1.建库: create database 库名称; 例如:create database mydata;(创建一个名为“mydata”的库): ...

  6. 【风马一族_Python】 决策树

    <机器学习实战>第三章 决策树 ------------------------------------- #1 trees.py 计算给定数据集的香农熵 ---------------- ...

  7. 13.mariadb-rhce考试解题思路

    1.安装mariadb ①yum install -y mariadb mariadb-server 或者 yum groupinstall -y mariadb 2.备份和还原数据库 ①备份:mys ...

  8. Arduino CNC Shiled 和 DRV8825驱动板的注意事项

    首先说明硬件:1) Arduino CNC Shiled V2.6 2)DRV8825驱动板 3)光驱步进电机  4)Arduino  uno R3 下图是本次主角是Arduino CNC Shile ...

  9. 《samba服务搭建》RHEL6

    Samba服务不仅可以实现linux和win之间的文件共享,也可以实现linux和linux之间的共享,samba的用户只限服务端本地用户使用. 本文的环境是selinux开启的情况下配置 Samba ...

  10. 《服务器的追踪与审计》RHEL6

    在linux系统/etc目录下有两个文件: 服务器的追踪: 当其他人访问我的主机时,通过日志监控到那台主机什么时间通过什么方式登陆,做什么?