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

来源:互联网 发布:淘宝卖奶粉需要什么 编辑:程序博客网 时间:2024/05/06 05:20

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

 

代码段一:

[cpp] view plaincopy
  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 plaincopy
  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 plaincopy
  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.csdn.net/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. }  
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 2岁宝宝总是不听话怎么办 2岁宝宝淘气不听话怎么办 两岁宝宝不听大人的话怎么办? 来月经奶量减少怎么办 月经来了奶少怎么办 来例假奶水少了怎么办 两岁宝宝吐口水怎么办 3岁宝宝不愿自己吃饭怎么办 婆家的人很烦人怎么办 三十了还没结婚怎么办 两岁宝贝断奶粉怎么办 宝宝断了母乳不吃奶粉怎么办 一岁宝宝不爱吃辅食怎么办 断奶后宝宝抗拒奶瓶怎么办 两岁宝宝断奶后不喝奶粉怎么办 两岁宝宝断奶不吃奶粉怎么办 宝宝断奶妈妈涨奶怎么办 三岁宝宝智商低怎么办 宝宝断奶晚上哭的厉害怎么办 2岁宝宝半夜喝奶粉怎么办 两岁宝宝不爱吃饭怎么办 快两岁的宝宝不爱吃饭怎么办 宝宝断奶后不愿意喝奶粉怎么办 宝宝断奶了不愿意喝奶怎么办? 宝宝断奶不愿意喝奶粉怎么办 宝宝断奶不愿意喝牛奶怎么办? 四个月宝宝断奶不吃奶粉怎么办 2岁不开口说话怎么办 八个月宝宝断奶不吃奶粉怎么办 宝宝断奶不喝奶粉怎么办 周岁 给娘家东西婆家看见怎么办 自己娘家妈总说婆家人坏话怎么办 娘家婆家老公都没有依靠怎么办? 2岁宝宝断奶粉怎么办 2岁宝宝夜奶频繁怎么办 宝宝15个月还在吃夜奶怎么办 宝宝两岁四个月还吃母乳怎么办 宝宝睡前老是找奶吃怎么办 宝宝戒奶晚上哭怎么办 宝宝戒奶半夜哭怎么办 吸习惯母乳不吸奶嘴怎么办