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。

#include <iostream>
#include <vector> using namespace std; int main()
{
vector<int> vec;
vec.push_back();
vec.push_back();
vec.push_back();
vec.push_back();
vec.push_back(); for(vector<int>::iterator it = vec.begin(); it != vec.end(); it++)
{
if(*it == )
{
it = vec.erase(it);
//vec.erase(it);
cout << *it << "CCC" << endl;
it --;
}
else
cout << *it << endl;
}
return ;
}

//注意上面不能写成

/*
        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. iOS数据持久化(二)SQLite

    一.什么是SQLite SQLite是一款轻型的嵌入式数据库,它占用资源非常的低,处理速度快,非常适合用于移动端开发. 二.使用 创建DataBaseHandle.h   &    DataB ...

  2. C语言获取系统时间的几种方式[转]

    C语言获取系统时间的几种方式 C语言中如何获取时间?精度如何? 1 使用time_t time( time_t * timer ) 精确到秒 2 使用clock_t clock() 得到的是CPU时间 ...

  3. java静态代码块、初始化块和构造方法的执行顺序

    分析:当执行new Child()时,它首先去看父类里面有没有静态代码块,如果有,它先去执行父类里面静态代码块里面的内容,当父类的静态代码块里面的内容执行完毕之后,接着去执行子类(自己这个类)里面的静 ...

  4. (转)国内外三个不同领域巨头分享的Redis实战经验及使用场景

    随着应用对高性能需求的增加,NoSQL逐渐在各大名企的系统架构中生根发芽.这里我们将为大家分享社交巨头新浪微博.传媒巨头Viacom及图片分享领域佼佼者Pinterest带来的Redis实践,首先我们 ...

  5. 如何给xml应用样式

    引子:可扩展标记语言xml(Extensible Markup Language)自己平常也用到的不多,除了在ajax处理服务器返回的数据可能会用到外(不过一般用json处理数据的比较常见)还真没怎么 ...

  6. 使用DbVisualizer 8 连接Oracle数据库

    1. 网上下载一个驱动包ojdbc14.jar,放到oracle目录下:...\DbVisualizer-8.0.1\jdbc\oracle\ojdbc14.jar 2. 打开 DbVisualize ...

  7. Java线程角度的内存模型和volatile型变量

    内存模型的目标是定义程序中各个变量的访问 规则,即在虚拟机中将变量(包括实例字段,静态字段和构成数组对象的元素,不包括局部变量与方法参数,因为后者是线程私有的)存储到内存和从内存中取出变量这样的底层细 ...

  8. !!! FAILED BINDER TRANSACTION !!! TransactionTooLargeException

    - ::): !!! FAILED BINDER TRANSACTION !!! xxxRecorder 运行40多分钟,崩溃,捕获日志 03-12 14:50:12.353: E/JavaBinde ...

  9. Entity Framework 并发处理借鉴

    如下博客 http://www.cnblogs.com/Bce-/p/3725868.html

  10. SQL Server中timestamp(时间戳)

    SQL Server timestamp 数据类型与时间和日期无关.SQL Server timestamp 是二进制数字,它表明数据库中数据修改发生的相对顺序.实现 timestamp 数据类型最初 ...