vector的earse造成迭代器失效的问题

一、提出

一般来说,在连续内存容器上插入和删除会使所有指向容器的迭代器、指针和引用失效。

对vector迭代器来说的几种失效的情况:

1.当插入(push_back)一个元素后,end操作返回的迭代器肯定失效。

2.当插入(push_back)一个元素后,capacity返回值与没有插入元素之前相比有改变,则需要重新加载整个容器,此时first和end操作返回的迭代器都会失效。

(capacity和szie的区别)

3.当进行删除操作(erase,pop_back)后,指向删除点的迭代器全部失效;指向删除点后面的元素的迭代器也将全部失效。

二、解决方法

个人认为用下面的方法就行了,至于用什么,随机应变了。

1.如果是单个元素的数组,或者只有一判断参数的其它类型数组,能用stl的bind这些的,或者可以用boost等等解决的,可以使用remove_if()。

2.如果是比较多的判断参数的,要么自己写仿函数,要么就直接用earse返回的下一个迭代。

三、代码测试

  1: //----------------------------------------------- 
  2: //说明:vector的earse测试 
  3: //环境:vs2008 
  4: //时间:2011-8-2 
  5: //作者:http://pppboy.blog.163.com
  6:  
  7: #include "stdafx.h" 
  8: #include <iostream> 
  9: #include <vector> 
 10: #include <algorithm> 
 11:  
 12: using namespace std; 
 13:  
 14: //----------------------------------------------- 
 15: void print(vector<int>& v) 
 16: {   
 17:   for (vector<int>::iterator it = v.begin(); it != v.end(); ++it) 
 18:   { 
 19:     cout << *it << " "; 
 20:   } 
 21:   cout << "\n------------------------------\n" ; 
 22: } 
 23:  
 24: //简单的判断参数 
 25: bool mod2(int i) 
 26: { 
 27:   return i % 2 == 0; 
 28: } 
 29:  
 30: //1.使用remove_if找出来,再earse掉 
 31: void doEarse1(vector<int>& v) 
 32: { 
 33:   vector<int>::iterator it = remove_if(v.begin(), v.end(), mod2); 
 34:   v.erase(it, v.end()); 
 35: } 
 36:  
 37: //2.使用earse返回的迭代器就是下一个迭代器 
 38: //for后面不加++it,因为可能earse完毕返回的正是end,会出错,而是在不earse时加 
 39: //适合结构体或者判断时需要多个参数的时候 
 40: void doEarse2(vector<int>& v) 
 41: { 
 42:  
 43:   for (vector<int>::iterator it = v.begin(); it != v.end(); ) 
 44:   { 
 45:     if (*it % 2 != 0) 
 46:     { 
 47:       //新的迭代 
 48:       it = v.erase(it); 
 49:     } 
 50:     else 
 51:     { 
 52:       //没有删除时才加 
 53:       ++it; 
 54:     } 
 55:   } 
 56: } 
 57:  
 58: //创建一个数组 
 59: vector<int> createVector() 
 60: { 
 61:   vector<int> v; 
 62:   for (int i = 0; i < 10; i++) 
 63:   { 
 64:     v.push_back(i); 
 65:   } 
 66:   return v; 
 67: } 
 68:  
 69: int main(int argc, char* argv[]) 
 70: { 
 71:   vector<int> v = createVector(); 
 72:   print(v); 
 73:   doEarse1(v); 
 74:   print(v); 
 75:  
 76:   v = createVector(); 
 77:   print(v); 
 78:   doEarse2(v); 
 79:   print(v); 
 80:  
 81:   system("pause"); 
 82:   return 0; 
 83: }

//输出

0 1 2 3 4 5 6 7 8 9 
------------------------------ 
1 3 5 7 9 
------------------------------ 
0 1 2 3 4 5 6 7 8 9 
------------------------------ 
0 2 4 6 8 
------------------------------ 
请按任意键继续. . .

四、声明

您可以任意引用,修改,演绎本文,但请保持文章完整性,不要误导他人,并给出引用链接。

本文出处:

 http://pppboy.blog.163.com/blog/static/302037962011720115935/

引用参考:

http://topic.csdn.net/u/20071127/14/9644163e-b7df-4a63-b4f7-7d72b9c906ec.html

http://topic.csdn.net/u/20090119/14/61c517a3-12e0-4b3e-8a93-31aa7c3a7814.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值