﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-guaguaman-随笔分类-转载的资料</title><link>http://www.cppblog.com/guaguaman/category/15417.html</link><description /><language>zh-cn</language><lastBuildDate>Wed, 10 Nov 2010 14:15:06 GMT</lastBuildDate><pubDate>Wed, 10 Nov 2010 14:15:06 GMT</pubDate><ttl>60</ttl><item><title>（转）vector中erase用法注意事项</title><link>http://www.cppblog.com/guaguaman/archive/2010/11/10/133240.html</link><dc:creator>赵氏呱呱儿</dc:creator><author>赵氏呱呱儿</author><pubDate>Wed, 10 Nov 2010 14:01:00 GMT</pubDate><guid>http://www.cppblog.com/guaguaman/archive/2010/11/10/133240.html</guid><wfw:comment>http://www.cppblog.com/guaguaman/comments/133240.html</wfw:comment><comments>http://www.cppblog.com/guaguaman/archive/2010/11/10/133240.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/guaguaman/comments/commentRss/133240.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/guaguaman/services/trackbacks/133240.html</trackback:ping><description><![CDATA[<p style="line-height: normal; color: rgb(51, 102, 102); font-family: Arial; font-size: 14px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">以前就发现了vector中的erase方法有些诡异(^_^)，稍不注意，就会出错。今天又一次遇到了，就索性总结一下，尤其是在循环体中用erase时，由于vector.begin() 和vector.end()是变化的，因此就引入了错误的可能性。</p><p style="line-height: normal; color: rgb(51, 102, 102); font-family: Arial; font-size: 14px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">vector&lt;int&gt; veci;<br style="line-height: normal; ">veci.push_back(1);<br style="line-height: normal; ">veci.push_back(2);<br style="line-height: normal; ">veci.push_back(3);<br style="line-height: normal; ">veci.push_back(4);<br style="line-height: normal; ">veci.push_back(5);<br style="line-height: normal; ">veci.push_back(3);<br style="line-height: normal; ">veci.push_back(2);<br style="line-height: normal; ">veci.push_back(3);</p><p style="line-height: normal; color: rgb(51, 102, 102); font-family: Arial; font-size: 14px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">for(vector&lt;int&gt;::iterator iter=veci.begin(); iter!=veci.end(); iter++)<br style="line-height: normal; ">{<br style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if( *iter == 3)<br style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; veci.erase(iter);<br style="line-height: normal; ">}</p><p style="line-height: normal; color: rgb(51, 102, 102); font-family: Arial; font-size: 14px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">乍一看这段代码，很正常。其实这里面隐藏着一个很严重的错误：当veci.erase(iter)之后，iter就变成了一个野指针，对一个野指针进行 iter++ 是肯定会出错的。</p><p style="line-height: normal; color: rgb(51, 102, 102); font-family: Arial; font-size: 14px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">查看MSDN，对于erase的返回值是这样描述的：An iterator that designates the first element remaining beyond any elements removed, or a pointer to the end of the vector if no such element exists，于是改代码：</p><p style="line-height: normal; color: rgb(51, 102, 102); font-family: Arial; font-size: 14px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">for(vector&lt;int&gt;::iterator iter=veci.begin(); iter!=veci.end(); iter++)<br style="line-height: normal; ">{<br style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if( *iter == 3)<br style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; iter = veci.erase(iter);<br style="line-height: normal; ">}</p><p style="line-height: normal; color: rgb(51, 102, 102); font-family: Arial; font-size: 14px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">这段代码也是错误的：1）无法删除两个连续的"3"； 2）当3位于vector最后位置的时候，也会出错（在veci.end()上执行 ++ 操作）</p><p style="line-height: normal; color: rgb(51, 102, 102); font-family: Arial; font-size: 14px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">正确的代码应该为：</p><p style="line-height: normal; color: rgb(51, 102, 102); font-family: Arial; font-size: 14px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">for(vector&lt;int&gt;::iterator iter=veci.begin(); iter!=veci.end(); )<br style="line-height: normal; ">{<br style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp; if( *iter == 3)<br style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; iter = veci.erase(iter);<br style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else<br style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; iter ++ ;<br style="line-height: normal; ">}</p>
<img src ="http://www.cppblog.com/guaguaman/aggbug/133240.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/guaguaman/" target="_blank">赵氏呱呱儿</a> 2010-11-10 22:01 <a href="http://www.cppblog.com/guaguaman/archive/2010/11/10/133240.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>