<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	
	>
<channel>
	<title>
	Comments on: How to Remove Elements from an Associative Container in C++	</title>
	<atom:link href="https://www.fluentcpp.com/2018/09/21/remove-elements-associative-container-cpp/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.fluentcpp.com/2018/09/21/remove-elements-associative-container-cpp/</link>
	<description>Jonathan Boccara&#039;s blog</description>
	<lastBuildDate>Sun, 07 Oct 2018 15:40:00 +0000</lastBuildDate>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.5.4</generator>
	<item>
		<title>
		By: Jonathan Boccara		</title>
		<link>https://www.fluentcpp.com/2018/09/21/remove-elements-associative-container-cpp/#comment-1372</link>

		<dc:creator><![CDATA[Jonathan Boccara]]></dc:creator>
		<pubDate>Sun, 07 Oct 2018 15:40:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=2968#comment-1372</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2018/09/21/remove-elements-associative-container-cpp/#comment-1316&quot;&gt;Ulf Benjaminsson&lt;/a&gt;.

Yes this seems like a good option to me!]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2018/09/21/remove-elements-associative-container-cpp/#comment-1316">Ulf Benjaminsson</a>.</p>
<p>Yes this seems like a good option to me!</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Jonathan Boccara		</title>
		<link>https://www.fluentcpp.com/2018/09/21/remove-elements-associative-container-cpp/#comment-1371</link>

		<dc:creator><![CDATA[Jonathan Boccara]]></dc:creator>
		<pubDate>Sun, 07 Oct 2018 15:38:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=2968#comment-1371</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2018/09/21/remove-elements-associative-container-cpp/#comment-1335&quot;&gt;Romain&lt;/a&gt;.

Thanks for the nice comment!
For the map on shared_ptr, how about using the erase_if function in the article, with a predicate checking if the value.use_count() == 1?]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2018/09/21/remove-elements-associative-container-cpp/#comment-1335">Romain</a>.</p>
<p>Thanks for the nice comment!<br />
For the map on shared_ptr, how about using the erase_if function in the article, with a predicate checking if the value.use_count() == 1?</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Jonathan Boccara		</title>
		<link>https://www.fluentcpp.com/2018/09/21/remove-elements-associative-container-cpp/#comment-1370</link>

		<dc:creator><![CDATA[Jonathan Boccara]]></dc:creator>
		<pubDate>Sun, 07 Oct 2018 15:36:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=2968#comment-1370</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2018/09/21/remove-elements-associative-container-cpp/#comment-1337&quot;&gt;Ulf Benjaminsson&lt;/a&gt;.

You&#039;re right. I try to reference posts in the &lt;a href=&quot;https://www.fluentcpp/posts/&quot; rel=&quot;nofollow&quot;&gt;Posts page&lt;/a&gt;, but they are references by category, in chronological order inside of the categories, but there is no global chronological order. I&#039;m trying to think about a way to better organize the contents of the website. I&#039;m open to any suggestion!]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2018/09/21/remove-elements-associative-container-cpp/#comment-1337">Ulf Benjaminsson</a>.</p>
<p>You&#8217;re right. I try to reference posts in the <a href="https://www.fluentcpp/posts/" rel="nofollow">Posts page</a>, but they are references by category, in chronological order inside of the categories, but there is no global chronological order. I&#8217;m trying to think about a way to better organize the contents of the website. I&#8217;m open to any suggestion!</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Jonathan Boccara		</title>
		<link>https://www.fluentcpp.com/2018/09/21/remove-elements-associative-container-cpp/#comment-1369</link>

		<dc:creator><![CDATA[Jonathan Boccara]]></dc:creator>
		<pubDate>Sun, 07 Oct 2018 15:33:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=2968#comment-1369</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2018/09/21/remove-elements-associative-container-cpp/#comment-1315&quot;&gt;Sergey Bastrakov&lt;/a&gt;.

Fixed, thanks!]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2018/09/21/remove-elements-associative-container-cpp/#comment-1315">Sergey Bastrakov</a>.</p>
<p>Fixed, thanks!</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Ulf Benjaminsson		</title>
		<link>https://www.fluentcpp.com/2018/09/21/remove-elements-associative-container-cpp/#comment-1337</link>

		<dc:creator><![CDATA[Ulf Benjaminsson]]></dc:creator>
		<pubDate>Fri, 28 Sep 2018 07:17:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=2968#comment-1337</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2018/09/21/remove-elements-associative-container-cpp/#comment-1315&quot;&gt;Sergey Bastrakov&lt;/a&gt;.

+1

Please add links to the previous (and future) articles in the series. It would be great if you could also add an arvhive of posts in chronological order - I find it incredibly difficult to locate old posts on this blog. Thanks!]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2018/09/21/remove-elements-associative-container-cpp/#comment-1315">Sergey Bastrakov</a>.</p>
<p>+1</p>
<p>Please add links to the previous (and future) articles in the series. It would be great if you could also add an arvhive of posts in chronological order &#8211; I find it incredibly difficult to locate old posts on this blog. Thanks!</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Romain		</title>
		<link>https://www.fluentcpp.com/2018/09/21/remove-elements-associative-container-cpp/#comment-1335</link>

		<dc:creator><![CDATA[Romain]]></dc:creator>
		<pubDate>Thu, 27 Sep 2018 19:20:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=2968#comment-1335</guid>

					<description><![CDATA[First, thank you ! Not only for this post but for the whole C++ blog. There&#039;s always something to learn on your post.

Now, it&#039;s time for an extra question :

I have a map (a kind of factory of object/ cache ), where the value is share_ptr and i like to sometimes &quot;purge&quot; my factory, ie i&#039;d like to erase_if the map-value is the last owner of the shared pointer...How can i do this in a clean way ?]]></description>
			<content:encoded><![CDATA[<p>First, thank you ! Not only for this post but for the whole C++ blog. There&#8217;s always something to learn on your post.</p>
<p>Now, it&#8217;s time for an extra question :</p>
<p>I have a map (a kind of factory of object/ cache ), where the value is share_ptr and i like to sometimes &#8220;purge&#8221; my factory, ie i&#8217;d like to erase_if the map-value is the last owner of the shared pointer&#8230;How can i do this in a clean way ?</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: jft		</title>
		<link>https://www.fluentcpp.com/2018/09/21/remove-elements-associative-container-cpp/#comment-1321</link>

		<dc:creator><![CDATA[jft]]></dc:creator>
		<pubDate>Fri, 21 Sep 2018 16:44:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=2968#comment-1321</guid>

					<description><![CDATA[&lt;pre&gt;&lt;code&gt;

for (auto it = begin(container); it != end(container); /* nothing here, the increment in dealt with inside the loop */ )
{
    if (shouldRemove(*it))
    {  
        it = container.erase(it);
    }
    else
    {
    ++it;
    }
}
&lt;/code&gt;&lt;/pre&gt;

This could be coded fluently as a 1 liner

&lt;pre&gt;&lt;code&gt;
for (auto it = begin(container); it != end(container); it = shouldRemove(*it) ? container.erase(it) : next(it));
&lt;/code&gt;&lt;/pre&gt;]]></description>
			<content:encoded><![CDATA[<pre><code>

for (auto it = begin(container); it != end(container); /* nothing here, the increment in dealt with inside the loop */ )
{
    if (shouldRemove(*it))
    {  
        it = container.erase(it);
    }
    else
    {
    ++it;
    }
}
</code></pre>
<p>This could be coded fluently as a 1 liner</p>
<pre><code>
for (auto it = begin(container); it != end(container); it = shouldRemove(*it) ? container.erase(it) : next(it));
</code></pre>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: jft		</title>
		<link>https://www.fluentcpp.com/2018/09/21/remove-elements-associative-container-cpp/#comment-1320</link>

		<dc:creator><![CDATA[jft]]></dc:creator>
		<pubDate>Fri, 21 Sep 2018 16:31:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=2968#comment-1320</guid>

					<description><![CDATA[Second part? Or are you counting starting at 0?


If a lot of erase-by-values are required for an associate container, then one way is to also have an inverse container. ie a multi-container that has a key of the value stored in the primary container and a value as an iterator to the entry in the primary container. This, obviously, uses a lot more memory and you have to be careful with operations on the primary container that may invalidate iterators (as the iterator value stored in the inverse container would then be invalid). .equal_range() can be used to obtain the range of iterators of the associate container corresponding to a specific key (ie value in the primary container) - and the values for these used to directly erase the elements in the primary - and then erased from the associate conatiner.



This way can add some code complexity, but where many erase-by-values are performed on large associate containers, it has definite performance advantages and should be considered.]]></description>
			<content:encoded><![CDATA[<p>Second part? Or are you counting starting at 0?</p>
<p>If a lot of erase-by-values are required for an associate container, then one way is to also have an inverse container. ie a multi-container that has a key of the value stored in the primary container and a value as an iterator to the entry in the primary container. This, obviously, uses a lot more memory and you have to be careful with operations on the primary container that may invalidate iterators (as the iterator value stored in the inverse container would then be invalid). .equal_range() can be used to obtain the range of iterators of the associate container corresponding to a specific key (ie value in the primary container) &#8211; and the values for these used to directly erase the elements in the primary &#8211; and then erased from the associate conatiner.</p>
<p>This way can add some code complexity, but where many erase-by-values are performed on large associate containers, it has definite performance advantages and should be considered.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Miguel Raggi		</title>
		<link>https://www.fluentcpp.com/2018/09/21/remove-elements-associative-container-cpp/#comment-1317</link>

		<dc:creator><![CDATA[Miguel Raggi]]></dc:creator>
		<pubDate>Fri, 21 Sep 2018 13:26:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=2968#comment-1317</guid>

					<description><![CDATA[unordered_set and unordered_map don&#039;t have &quot;Comparators&quot;... they have hashes (in the last code)]]></description>
			<content:encoded><![CDATA[<p>unordered_set and unordered_map don&#8217;t have &#8220;Comparators&#8221;&#8230; they have hashes (in the last code)</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Ulf Benjaminsson		</title>
		<link>https://www.fluentcpp.com/2018/09/21/remove-elements-associative-container-cpp/#comment-1316</link>

		<dc:creator><![CDATA[Ulf Benjaminsson]]></dc:creator>
		<pubDate>Fri, 21 Sep 2018 12:44:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=2968#comment-1316</guid>

					<description><![CDATA[I suspect we can simplify your erase_if slightly by making use of std::find_if in the loop header and avoid the branching body. Something like this perhaps? 

&lt;pre&gt;
 &lt;code class=&quot;c++&quot;&gt;
template &#060;class AssociativeContainer, class Predicate&#062;
static void remove_if(AssociativeContainer&#038; c, Predicate pred) {
	using Iterator = typename AssociativeContainer::iterator;
	Iterator cur = std::begin(c);
	const Iterator last = std::end(c);
	while ((cur = std::find_if(cur, last, pred)) != last) {
		Iterator tmp = cur++;
		c.erase(tmp);
	}
}
&lt;/code&gt;
&lt;/pre&gt;]]></description>
			<content:encoded><![CDATA[<p>I suspect we can simplify your erase_if slightly by making use of std::find_if in the loop header and avoid the branching body. Something like this perhaps? </p>
<pre>
 <code class="c++">
template &#060;class AssociativeContainer, class Predicate&#062;
static void remove_if(AssociativeContainer&amp; c, Predicate pred) {
	using Iterator = typename AssociativeContainer::iterator;
	Iterator cur = std::begin(c);
	const Iterator last = std::end(c);
	while ((cur = std::find_if(cur, last, pred)) != last) {
		Iterator tmp = cur++;
		c.erase(tmp);
	}
}
</code>
</pre>
]]></content:encoded>
		
			</item>
	</channel>
</rss>
