别被vector最后一个元素erase错误

前言:

vector我们经常使用,对vector里面的基本函数构造函数、增加函数、删除函数、遍历函数我们也会用到。其中在使用遍历之后erase删除元素过程中,会出现一种删除最后一个元素破坏了迭代器的情况。

如下所示 删除到最后一个元素的时候就会报错

vector<int> data(10);
auto temp_begin = data.begin(), temp_end= data.end();
for(;temp_begin!=temp_end;){
    data.erase(temp_begin);
}

产生这个问题的原因是:当我们调用erase方法删除元素的时候,erase方法会返回下一个元素的迭代器。当删除到最后一个元素的时候,这个时候返回的是最后一个元素之后的容器,而不是最后一个元素。

作者:良知犹存

转载授权以及围观:欢迎关注微信公众号:羽林君

或者添加作者个人微信:become_me


原因分析:

vector:

Because vectors keep an array format, erasing on positions other than the vector end also moves all the elements after the segment erased to their new positions, which may not be a method as efficient as erasing in other kinds of sequence containers (deque, list). This invalidates all iterator and references to position (or first) and its subsequent elements.

假设你的 vector 中有多个对象,最后一个被标记为销毁。当您调用erase时,它将返回一个新的有效迭代器,该迭代器指向已删除元素之后的元素。被删除的元素之后没有元素,因此返回的迭代器为data.end()。

然后,我们继续循环的顶部并取消引用此迭代器,这是无效的。如果要让迭代器指向有效元素,则需要在擦除后减少其迭代器。vec.end() 给你元素的迭代器以下容器的最后一个元素。看这里:

list和vector区别(list 这样删除就没事)

List:

This effectively reduces the list size by the number of elements removed, calling each element's destructor before.

lists are sequence containers specifically designed to be efficient inserting and removing elements in any position, even in the middle of the sequence. Compared to the other base sequence containers (vector and deque), lists are the most efficient container erasing at some position other than the beginning or the end of the sequence, and, unlike in these, all of the previously obtained iterators and references remain valid after the erasing operation and refer to the same elements they were referring before (except, naturally, for those referring to erased elements).

列表是序列容器,专门设计用于在任何位置高效插入和删除元素,甚至在序列的中间。list erase不会改变原来的iterator,所以不会出现像vector删除最后一个iterator后程序错误。

修改建议

下面修改的建议都是让vector的end迭代器可以一直更新,重新判断当前位置。

vector<int> data(10);
auto iter = data.begin();
while(iter != data.end())
{
  data.erase(iter);
}
for (iter = data.begin(); it != data.end();)
{
    data.erase(iter);
}

将删除最后一个元素

data.erase(data.end() - 1); 


 data.erase(data.begin() + data.size() - 1); 

应该提到的是,如果向量为空,则该操作将崩溃,因此出于安全考虑,我们可以再添加一个vector是否为空的判断

if (!data.empty())
  data.erase(vec.end() - 1);

另外我们也发现 vector 的 erase 需要整个vector 移动,这个代价十分高,所以尽量少用。若排序顺序不是很重要的话,可以和最后的那个item swap,然后删掉最后那个,这样可以显著的提高效率。

结语

这就是我分享的项目中一些vector使用,如果大家有更好的想法和需求,也欢迎大家加我好友交流分享哈。


作者:良知犹存,白天努力工作,晚上原创公号号主。公众号内容除了技术还有些人生感悟,一个认真输出内容的职场老司机,也是一个技术之外丰富生活的人,摄影、音乐 and 篮球。关注我,与我一起同行。

                                                ‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧  END  ‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧

推荐阅读

【1】C++的智能指针你了解吗?

【2】嵌入式底层开发的软件框架简述

【3】CPU中的程序是怎么运行起来的 必读

【4】cartographer环境建立以及建图测试

【5】设计模式之简单工厂模式、工厂模式、抽象工厂模式的对比

本公众号全部原创干货已整理成一个目录,回复[ 资源 ]即可获得。

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

御林你好

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值