正确使用stl vecotr erase函数

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(1);
    vec.push_back(2);
    vec.push_back(3);
    vec.push_back(4);
    vec.push_back(5);

    for(vector<int>::iterator it = vec.begin(); it != vec.end(); it++)
    {
        if(*it == 3)
        {
            it = vec.erase(it);
            //vec.erase(it);
            cout << *it << "CCC" << endl;
            it --;
        }
        else
            cout << *it << endl;
    }
    return 0;
}

//注意上面不能写成

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

}
        }
      
    */

时间: 2024-08-01 14:27:50

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

C++.stl map::erase陷阱

map::erase函数在不同版本stl中的差异 1. C++98和C++11标准 http://www.cplusplus.com/reference/map/map/erase/ 2. pj stl(windows) map::erase函数的windows实现版本(C++11标准)会返回一个map::iterator: iterator map::erase(const_iterator _Where); iterator map::erase(const_iterator _First,

正确使用stl map的erase方法

先声明:下面的文章是针对windows的用法,因为std::map的erase函数的windows的实现版本是返回一个std::map的迭代器,但是STL标准里面的该函数的返回值确是: map.erase有3个重载:void erase ( iterator position );size_type erase ( const key_type& x );void erase ( iterator first, iterator last ); . 所以下面的代码中的最后一个例子仅仅可以在win

正确使用stl map的find,erase方法

查找某个key对应的value的方法,有2种: 方法1: typedef std::multimap<boost::uint64_t,std::string>::iterator it it = mmap_.find( nCallLetter ); if ( it != mmap_.end()) { char key[20] = {0}; sprintf(key, "%I64d",it->first); qDebug("key = %s,value = %s

map中erase函数的使用

STL的map表里有一个erase方法用来从一个map中删除掉指令的节点eg:map<string,string> mapTest;typedef map<string,string>::iterator ITER; ITER iter=mapTest.find(key);mapTest.erase(iter); 像上面这样只是删除单个节点,map的形为不会出现任务问题,但是当在一个循环里用的时候,往往会被误用,那是因为使用者没有正确理解iterator的概念.像下面这样的一个例子

STL中erase的小心使用

先看如下一道改错题: #include<iostream> #include<vector> using namespace std; void print(vector<int>); int main() { vector<int> array; array.push_back(1); array.push_back(6); array.push_back(6); array.push_back(3); //删除array数组中所有的6 vector<

C++中erase函数的使用,可以用来删除内存擦除

erase函数的原型如下:(1)string& erase ( size_t pos = 0, size_t n = npos );(2)iterator erase ( iterator position );(3)iterator erase ( iterator first, iterator last );也就是说有三种用法:(1)erase(pos,n); 删除从pos开始的n个字符,比如erase(0,1)就是删除第一个字符(2)erase(position);删除position处

c++ STL unique , unique_copy函数

一.unique函数 类属性算法unique的作用是从输入序列中"删除"全部相邻的反复元素. 该算法删除相邻的反复元素.然后又一次排列输入范围内的元素,而且返回一个迭代器(容器的长度没变,仅仅是元素顺序改变了),表示无反复的值范围得结束. // sort words alphabetically so we can find the duplicates sort(words.begin(), words.end()); /* eliminate duplicate words: *

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

STL区间成员函数及区间算法总结 在这里总结下可替代循环的区间成员函数和区间算法: 相比单元素遍历操作,使用区间成员函数的优势在于: 1)更少的函数调用 2)更少的元素移动 3)更少的内存分配 在区间成员函数不适用的情况下也应该使用区间算法,至少,相比手写循环而言,它更加简单,有效,并且不容易出错: 区间成员函数 区间构造 标准容器都支持区间构造函数: container::container(InputIterator begin, // 区间的起点 InputIterator end); /

multiset容器erase函数的误用

<从缺陷中学习C/C++>第3章库函数问题,本章主要介绍库函数的使用中会遇到的问题.使用库函数可以降低软件开发的难度,提高代码编写的效率.本节为大家介绍multiset容器erase函数的误用. AD: 51CTO 网+ 第十二期沙龙:大话数据之美_如何用数据驱动用户体验 3.16  multiset容器erase函数的误用 代码示例 int main(){ multiset <int> c1; c1.insert(3); c1.insert(2); c1.insert(3); c