怎样正确 移除迭代器所指元素

本文深入探讨了C++标准库中迭代器的使用技巧及常见错误案例,通过三个具体代码段,展示了如何正确地利用迭代器进行元素查找与删除操作,同时揭示了可能导致的运行时和编译时错误,提供了详细的分析和解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以下三段代码摘自《C++ 标准程序库》p204-205

 

代码段一:

[cpp]  view plain copy
  1. typedef std::map<std::string, float> StringFloatMap;  
  2. StringFloatMap coll;  
  3. StringFloatMap::iterator pos;  
  4. //...  
  5. for (pos = coll.begin(); pos != coll.end(); ++pos)  
  6. {  
  7.  if (pos->second == value)  
  8.  {  
  9.   coll.erase(pos);   // RUNTIME ERROR !!!  
  10.  }  
  11. }  


 

代码段 二:

[cpp]  view plain copy
  1. // 如果 erase() 总是返回下一元素的位置, 那就好办了,如下  
  2. typedef std::map<std::string, float> StringFloatMap;  
  3. StringFloatMap coll;  
  4. StringFloatMap::iterator pos;  
  5. //...  
  6. for (pos = coll.begin(); pos != coll.end(); )  
  7. {  
  8.  if (pos->second == value)  
  9.  {  
  10.   pos = coll.erase(pos); // would be fine, but COMPLILE TIME ERROR!!!  
  11.  }  
  12.  else  
  13.  {  
  14.   ++ pos;  
  15.  }  
  16. }  


 

代码段三: 完整且正确的哦

[cpp]  view plain copy
  1. #include <vector>  
  2. #include <string>  
  3. #include <iostream>  
  4. #include <map>  
  5. using namespace std;  
  6.   
  7. int _tmain(int argc, _TCHAR* argv[])  
  8. {  
  9.     typedef map<string, int> StringIntMap;  
  10.     StringIntMap coll;  
  11.   
  12.     coll.insert(make_pair("one", 1));  
  13.     coll.insert(make_pair("two", 2));  
  14.     coll.insert(make_pair("three", 3));  
  15.     coll.insert(make_pair("four", 4));  
  16.   
  17.   
  18.     {   
  19.         cout << "称除元素前: " << endl;  
  20.         StringIntMap::iterator itBeg = coll.begin();  
  21.         StringIntMap::iterator itEnd = coll.end();  
  22.         for (NULL; itBeg != itEnd; ++itBeg)  
  23.         {  
  24.             cout << itBeg->first << "---> " << itBeg->second << endl;  
  25.         }  
  26.     }    
  27.   
  28.   
  29.     {   // 重点看这一段 ************************************************************  
  30.           
  31.         // 移除值为"2" 的项  
  32.         StringIntMap::iterator itBeg = coll.begin();  
  33.         StringIntMap::iterator itEnd = coll.end();  
  34.         for (NULL; itBeg != itEnd; )  
  35.         {  
  36.             if (itBeg->second == 2)  
  37.             {  
  38.                 coll.erase(itBeg ++ );    // 重点分析这一句:  
  39.             }  
  40.             else  
  41.             {  
  42.                 ++itBeg;  
  43.             }  
  44.         }  
  45.   
  46.          // 分析: 注意这一句相当于  
  47.         //         1.StringIntMap::iterator itTmp = itBeg;  
  48.         //         2. itBeg 自增.指向下一个元素  
  49.         //         3. coll.erase(itTmp);  // 注意哦,调用erase后 迭代器无效, 但是这里是临时的对象,不怕....  
  50.         //    更详解决可看 标准库的源码,或者看类似的解释 http://blog.youkuaiyun.com/w_sx12553/article/details/8507188  
  51.   
  52.   
  53.     }   // 重点看这一段 ************************************************************  
  54.   
  55.   
  56.     cout << endl << "称除元素后: " << endl;  
  57.     {  
  58.         StringIntMap::iterator itBeg = coll.begin();  
  59.         StringIntMap::iterator itEnd = coll.end();  
  60.         for (NULL; itBeg != itEnd; ++itBeg)  
  61.         {  
  62.             cout << itBeg->first << "---> " << itBeg->second << endl;  
  63.         }  
  64.     }    
  65.   
  66.     return 0;  
  67. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值