<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ClearChain &#187; c++</title>
	<atom:link href="http://www.clearchain.com/blog/tags/c/feed" rel="self" type="application/rss+xml" />
	<link>http://www.clearchain.com/blog</link>
	<description>-= Daily Happenings =-</description>
	<lastBuildDate>Wed, 05 Oct 2011 23:02:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Stl::reverse iterator</title>
		<link>http://www.clearchain.com/blog/posts/stlreverse-iterator</link>
		<comments>http://www.clearchain.com/blog/posts/stlreverse-iterator#comments</comments>
		<pubDate>Thu, 13 Nov 2008 14:21:10 +0000</pubDate>
		<dc:creator>Benjamin Close</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[iterator]]></category>
		<category><![CDATA[stl]]></category>

		<guid isPermaLink="false">http://www.clearchain.com/blog/?p=121</guid>
		<description><![CDATA[<a href="http://www.clearchain.com/blog/posts/stlreverse-iterator"><img align="left" hspace="5" width="150" height="150" src="http://www.clearchain.com/blog/wp-content/plugins/thumbnail-for-excerpts/tfe_no_thumb.png" class="alignleft wp-post-image tfe" alt="" title="" /></a>I was coding a little program a while back when I found a Quirk about the stl reverse_iterator. Use of the iterator is quite easy until you have to delete from one. I tried the normal way of: vector&#60;sometype&#62;::reverse_iterator myiterator... .. .. myvector.erase(myiterator). only to find the code didn&#8217;t compile. A little baffled I started<a href="http://www.clearchain.com/blog/posts/stlreverse-iterator"> <font size=-2>[..more..]</font></a>]]></description>
			<content:encoded><![CDATA[<p>I was coding a little program a while back when I found a <a title="Category:Programming Quirks" href="http://www.clearchain.com/blog/categories/computers/programming">Quirk</a> about the <code>stl  reverse_iterator</code>. Use of the iterator is quite easy until you have to delete from one. I tried the normal way of:</p>
<pre>vector&lt;sometype&gt;::reverse_iterator myiterator...
..
..
myvector.erase(myiterator).</pre>
<p>only to find the code didn&#8217;t compile. A little baffled I started to looking into why. It turns out you <strong>Can&#8217;t&#8217;<em> delete from a reverse iterator directly. So the question is </em>How do you delete from a reverse iterator?</strong>.</p>
<p>Not wanting to do a full forward traversal to get to the same point I did some <a class="external text" title="http://www.google.com" rel="nofollow" href="http://www.google.com/">Googling</a>. Eventually I found the page: <a class="external free" title="http://www.ddj.com/dept/cpp/184401406" rel="nofollow" href="http://www.ddj.com/dept/cpp/184401406">http://www.ddj.com/dept/cpp/184401406</a></p>
<p>which explains why you can&#8217;t delete from the reverse iterator. More importantly it indicates a way that you can delete from the reverse_iterator. So how?</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"> <span style="color: #993333;">void</span> Manager<span style="color: #339933;">::</span><span style="color: #202020;">raiseWindow</span><span style="color: #009900;">&#40;</span>WMWindow<span style="color: #339933;">*</span> wmwindow<span style="color: #009900;">&#41;</span>
 <span style="color: #009900;">&#123;</span>
   <span style="color: #339933;">&lt;</span>strong<span style="color: #339933;">&gt;</span>vector<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>WMWindow<span style="color: #339933;">*&amp;</span>gt<span style="color: #339933;">;::</span><span style="color: #202020;">reverse_iterator</span> it <span style="color: #339933;">=</span> windows.<span style="color: #202020;">rbegin</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;&lt;/</span>strong<span style="color: #339933;">&gt;</span>
   <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span>it <span style="color: #339933;">!=</span> windows.<span style="color: #202020;">rend</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
     <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>it <span style="color: #339933;">==</span> wmwindow<span style="color: #009900;">&#41;</span>
     <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">// why ++it see &lt;a class=&quot;external free&quot; title=&quot;http://www.ddj.com/dept/cpp/184401406&quot; rel=&quot;nofollow&quot; href=&quot;http://www.ddj.com/dept/cpp/184401406&quot;&gt;http://www.ddj.com/dept/cpp/184401406&lt;/a&gt;</span>
      <span style="color: #339933;">&lt;</span>strong<span style="color: #339933;">&gt;</span>windows.<span style="color: #202020;">erase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">++</span>it<span style="color: #009900;">&#41;</span>.<span style="color: #202020;">base</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;&lt;/</span>strong<span style="color: #339933;">&gt;</span>
      windows.<span style="color: #202020;">push_back</span><span style="color: #009900;">&#40;</span>wmwindow<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
     it<span style="color: #339933;">++;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   wmwindow<span style="color: #339933;">-&gt;</span>raise<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Gives an example of how to do it. However, be aware, doing the above erase, invalidates the iterator so don&#8217;t go trying to use it again afterwards! }</p>
]]></content:encoded>
			<wfw:commentRss>http://www.clearchain.com/blog/posts/stlreverse-iterator/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

