自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

別被Vector最后一個(gè)元素Erase錯(cuò)誤

開(kāi)發(fā) 前端
vector我們經(jīng)常使用,對(duì)vector里面的基本函數(shù)構(gòu)造函數(shù)、增加函數(shù)、刪除函數(shù)、遍歷函數(shù)我們也會(huì)用到。其中在使用遍歷之后erase刪除元素過(guò)程中,會(huì)出現(xiàn)一種刪除最后一個(gè)元素破壞了迭代器的情況。

[[419557]]

前言:

vector我們經(jīng)常使用,對(duì)vector里面的基本函數(shù)構(gòu)造函數(shù)、增加函數(shù)、刪除函數(shù)、遍歷函數(shù)我們也會(huì)用到。其中在使用遍歷之后erase刪除元素過(guò)程中,會(huì)出現(xiàn)一種刪除最后一個(gè)元素破壞了迭代器的情況。

如下所示 刪除到最后一個(gè)元素的時(shí)候就會(huì)報(bào)錯(cuò):

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

產(chǎn)生這個(gè)問(wèn)題的原因是:當(dāng)我們調(diào)用erase方法刪除元素的時(shí)候,erase方法會(huì)返回下一個(gè)元素的迭代器。當(dāng)刪除到最后一個(gè)元素的時(shí)候,這個(gè)時(shí)候返回的是最后一個(gè)元素之后的容器,而不是最后一個(gè)元素。

原因分析:

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.

假設(shè)你的 vector 中有多個(gè)對(duì)象,最后一個(gè)被標(biāo)記為銷(xiāo)毀。當(dāng)您調(diào)用erase時(shí),它將返回一個(gè)新的有效迭代器,該迭代器指向已刪除元素之后的元素。被刪除的元素之后沒(méi)有元素,因此返回的迭代器為data.end()。

然后,我們繼續(xù)循環(huán)的頂部并取消引用此迭代器,這是無(wú)效的。如果要讓迭代器指向有效元素,則需要在擦除后減少其迭代器。vec.end() 給你元素的迭代器以下容器的最后一個(gè)元素。看這里:

list和vector區(qū)別(list 這樣刪除就沒(méi)事)

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).

列表是序列容器,專(zhuān)門(mén)設(shè)計(jì)用于在任何位置高效插入和刪除元素,甚至在序列的中間。list erase不會(huì)改變?cè)瓉?lái)的iterator,所以不會(huì)出現(xiàn)像vector刪除最后一個(gè)iterator后程序錯(cuò)誤。

修改建議

下面修改的建議都是讓vector的end迭代器可以一直更新,重新判斷當(dāng)前位置。

  1. vector<int> data(10); 
  2. auto iter = data.begin(); 
  3. while(iter != data.end()) 
  4.   data.erase(iter); 
  1. for (iter = data.begin(); it != data.end();) 
  2.     data.erase(iter); 

將刪除最后一個(gè)元素

  1. data.erase(data.end() - 1);  
  2.  
  3.  
  4.  data.erase(data.begin() + data.size() - 1);  

應(yīng)該提到的是,如果向量為空,則該操作將崩潰,因此出于安全考慮,我們可以再添加一個(gè)vector是否為空的判斷

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

另外我們也發(fā)現(xiàn) vector 的 erase 需要整個(gè)vector 移動(dòng),這個(gè)代價(jià)十分高,所以盡量少用。若排序順序不是很重要的話,可以和最后的那個(gè)item swap,然后刪掉最后那個(gè),這樣可以顯著的提高效率。

結(jié)語(yǔ)

這就是我分享的項(xiàng)目中一些vector使用,如果大家有更好的想法和需求,也歡迎大家加我好友交流分享哈。 

作者:良知猶存,白天努力工作,晚上原創(chuàng)公號(hào)號(hào)主。公眾號(hào)內(nèi)容除了技術(shù)還有些人生感悟,一個(gè)認(rèn)真輸出內(nèi)容的職場(chǎng)老司機(jī),也是一個(gè)技術(shù)之外豐富生活的人,攝影、音樂(lè) and 籃球。關(guān)注我,與我一起同行。

責(zé)任編輯:武曉燕 來(lái)源: 羽林君
相關(guān)推薦

2024-06-06 08:46:26

彈性布局元素瀏覽器

2021-12-13 11:31:36

排序數(shù)組數(shù)據(jù)結(jié)構(gòu)算法

2021-11-02 14:54:41

排序數(shù)組元素

2014-05-20 09:54:20

2012-06-15 14:58:01

諾基亞

2022-04-28 09:05:41

網(wǎng)絡(luò)爬蟲(chóng)Python

2013-04-08 11:34:10

Ubuntu

2022-12-09 08:23:01

2021-06-16 17:46:55

函數(shù)指針結(jié)構(gòu)

2020-12-13 11:09:58

2019-10-31 13:58:32

阿里電商系統(tǒng)

2017-09-13 14:38:55

USB散熱器機(jī)箱

2019-07-29 09:25:01

云優(yōu)先工作負(fù)載公共云

2009-08-06 16:25:28

云計(jì)算總舵主

2010-09-02 13:39:32

iOS 4.1iPhone

2018-06-27 13:10:22

程序員面試易犯錯(cuò)誤

2024-03-18 09:50:18

Selenium元素Python

2024-06-03 10:07:22

Vector類(lèi)元素向量

2020-01-15 17:16:56

Windows 7補(bǔ)丁安全更新

2015-08-17 15:42:43

Netflix公有云關(guān)閉數(shù)據(jù)中心
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)