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. 【转载】#274 - Can't Overload if Methods Differ Only by ref and out Modifiers

    You can overload a method in a class, i.e. define two methods with the same name, if the methods hav ...

  2. (转)在 Windows 上安装Rabbit MQ 指南

    rabbitMQ是一个在AMQP协议标准基础上完整的,可服用的企业消息系统.他遵循Mozilla Public License开源协议.采用 Erlang 实现的工业级的消息队列(MQ)服务器. Ra ...

  3. 胸腺嘧啶“T”

    4.下列物质或结构中含胸腺嘧啶“T”的是( )A.DNA聚合酶 B.烟草花叶病毒C.ATP D.B淋巴细胞中的线粒体

  4. NSS_04 extjs中grid接收datetime类型参数列

    今天在做用户列表时发现, asp.net mvc3的控制器在返回JsonResult结果时, 会把对象内的DateTime类型成员,解析为类似于\/Date(1238606590509)\/的格式 , ...

  5. MongoDB五种树形结构表示法

    MongoDB五种树形结构表示法 第一种:父链接结构 db.categories.insert( { _id: "MongoDB", parent: "Databases ...

  6. 卸载CentOS 5.4自带的OpenJDK,配置新的Java环境

    本文CentOS版本为5.4 final,使用图形界面与命令结合的操作方式,由于CentOS 5.4在默认情况下,会安装OpenOffice之类的软件,而这些软件需要Java支持,因此系统会默认安装一 ...

  7. MIT Scheme 的基本使用

    MIT Scheme 的基本使用 安装和启动 启动 在 Windows 下正确安装 MIT Scheme 系统后,程序菜单里将有一个 MIT Scheme 目录,其中包含: Documentation ...

  8. [转]Linux中find常见用法示例

    Linux中find常见用法示例[转]·find   path   -option   [   -print ]   [ -exec   -ok   command ]   {} \;find命令的参 ...

  9. [转]MAC下JDK版本的切换

    系统里之前先安装里jdk6的,后台又装里7,安装完成后,java -version 版本是7,  导致我eclipse打不开,一开始的做法是,把7的版本给删除掉. 删除的方法也很简单,在命令行中到 / ...

  10. WPF学习06:转换控件内容为可存储图片

    在图形软件中,我们经常使用到"另存为图片"的功能,本文即介绍如何将WPF控件显示内容转换为图片. , , PixelFormats.Pbgra32); bitmapRender.R ...