<?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 Pointers from a Vector in C++	</title>
	<atom:link href="https://www.fluentcpp.com/2018/09/18/how-to-remove-pointers-from-a-vector-in-cpp/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.fluentcpp.com/2018/09/18/how-to-remove-pointers-from-a-vector-in-cpp/</link>
	<description>Jonathan Boccara&#039;s blog</description>
	<lastBuildDate>Sun, 07 Oct 2018 15:32:10 +0000</lastBuildDate>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.5.4</generator>
	<item>
		<title>
		By: Marek Kurdej		</title>
		<link>https://www.fluentcpp.com/2018/09/18/how-to-remove-pointers-from-a-vector-in-cpp/#comment-1333</link>

		<dc:creator><![CDATA[Marek Kurdej]]></dc:creator>
		<pubDate>Thu, 27 Sep 2018 07:18:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=4275#comment-1333</guid>

					<description><![CDATA[My 5 cents... It would be cool if you used `std::default_delete` in your examples instead of rolling out your own custom lambda that just calls `delete`.]]></description>
			<content:encoded><![CDATA[<p>My 5 cents&#8230; It would be cool if you used `std::default_delete` in your examples instead of rolling out your own custom lambda that just calls `delete`.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: jft		</title>
		<link>https://www.fluentcpp.com/2018/09/18/how-to-remove-pointers-from-a-vector-in-cpp/#comment-1323</link>

		<dc:creator><![CDATA[jft]]></dc:creator>
		<pubDate>Sun, 23 Sep 2018 09:26:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=4275#comment-1323</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2018/09/18/how-to-remove-pointers-from-a-vector-in-cpp/#comment-1322&quot;&gt;Uri Goren&lt;/a&gt;.

Testing. and examination of the stl code for remove_if for MS VS, confirms that the predicate is indeed invoked just once for each iterator in the specified range. I would expect other stl libraries to exhibit the same behaviour.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2018/09/18/how-to-remove-pointers-from-a-vector-in-cpp/#comment-1322">Uri Goren</a>.</p>
<p>Testing. and examination of the stl code for remove_if for MS VS, confirms that the predicate is indeed invoked just once for each iterator in the specified range. I would expect other stl libraries to exhibit the same behaviour.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Uri Goren		</title>
		<link>https://www.fluentcpp.com/2018/09/18/how-to-remove-pointers-from-a-vector-in-cpp/#comment-1322</link>

		<dc:creator><![CDATA[Uri Goren]]></dc:creator>
		<pubDate>Sat, 22 Sep 2018 11:17:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=4275#comment-1322</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2018/09/18/how-to-remove-pointers-from-a-vector-in-cpp/#comment-1311&quot;&gt;jft&lt;/a&gt;.

You&#039;re abusing the predicate. A predicate should provide a Boolean answer, without side effects.
Is it guaranteed that the predicate is invoked just once? I have no idea why am implementation would call it twice, but you don&#039;t want to rely on it.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2018/09/18/how-to-remove-pointers-from-a-vector-in-cpp/#comment-1311">jft</a>.</p>
<p>You&#8217;re abusing the predicate. A predicate should provide a Boolean answer, without side effects.<br />
Is it guaranteed that the predicate is invoked just once? I have no idea why am implementation would call it twice, but you don&#8217;t want to rely on it.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: jft		</title>
		<link>https://www.fluentcpp.com/2018/09/18/how-to-remove-pointers-from-a-vector-in-cpp/#comment-1311</link>

		<dc:creator><![CDATA[jft]]></dc:creator>
		<pubDate>Wed, 19 Sep 2018 07:38:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=4275#comment-1311</guid>

					<description><![CDATA[vec.erase(std::remove_if(vec.begin(), vec.end(), [](int* pi){ return *pi % 2 == 0; }), vec.end());

Unless there are special circumstances (eg duplicates), why not simply

vec.erase(std::remove_if(vec.begin(), vec.end(), [](int* pi) { 
    if (*pi %2 == 0) {
        delete pi;
        return true;
    }
    return false; }
), vec.end());

This will delete the memory as part of the remove_if() lambda - so one iteration for the container and one condition test per element.]]></description>
			<content:encoded><![CDATA[<p>vec.erase(std::remove_if(vec.begin(), vec.end(), [](int* pi){ return *pi % 2 == 0; }), vec.end());</p>
<p>Unless there are special circumstances (eg duplicates), why not simply</p>
<p>vec.erase(std::remove_if(vec.begin(), vec.end(), [](int* pi) {<br />
    if (*pi %2 == 0) {<br />
        delete pi;<br />
        return true;<br />
    }<br />
    return false; }<br />
), vec.end());</p>
<p>This will delete the memory as part of the remove_if() lambda &#8211; so one iteration for the container and one condition test per element.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: jft		</title>
		<link>https://www.fluentcpp.com/2018/09/18/how-to-remove-pointers-from-a-vector-in-cpp/#comment-1310</link>

		<dc:creator><![CDATA[jft]]></dc:creator>
		<pubDate>Tue, 18 Sep 2018 10:20:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=4275#comment-1310</guid>

					<description><![CDATA[If the raw pointers held by the vector are not unique (which in some cases is certainly possible - either an address value from new or a previously allocated address), then you have to be careful not to perform multiple deletes for the same pointer value - which is undefined behaviour. The easiest way for this is to sort the vector by raw pointer value and then use unique() to remove duplicates. Then perform the required deletions - however this changes the order of the vector elements which may be undesirable. Another way is to iterate the vector and maintain a set of deleted raw pointer values - and only delete if the raw pointer element isn&#039;t present in the set.


Also, it is assumed that the raw pointer points to just one value hence delete is used - rather than pointing to an array where delete [] would need to be used. There is also the potential that the memory was allocated by malloc()/calloc() rather than new, so free() would need to be used rather than delete (probably not common but I&#039;ve seen examples of this even in quite recent c++ code!!).


Re-iterating the point already made - a container of owning raw pointers is a seriously bad idea.]]></description>
			<content:encoded><![CDATA[<p>If the raw pointers held by the vector are not unique (which in some cases is certainly possible &#8211; either an address value from new or a previously allocated address), then you have to be careful not to perform multiple deletes for the same pointer value &#8211; which is undefined behaviour. The easiest way for this is to sort the vector by raw pointer value and then use unique() to remove duplicates. Then perform the required deletions &#8211; however this changes the order of the vector elements which may be undesirable. Another way is to iterate the vector and maintain a set of deleted raw pointer values &#8211; and only delete if the raw pointer element isn&#8217;t present in the set.</p>
<p>Also, it is assumed that the raw pointer points to just one value hence delete is used &#8211; rather than pointing to an array where delete [] would need to be used. There is also the potential that the memory was allocated by malloc()/calloc() rather than new, so free() would need to be used rather than delete (probably not common but I&#8217;ve seen examples of this even in quite recent c++ code!!).</p>
<p>Re-iterating the point already made &#8211; a container of owning raw pointers is a seriously bad idea.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Sergey Bastrakov		</title>
		<link>https://www.fluentcpp.com/2018/09/18/how-to-remove-pointers-from-a-vector-in-cpp/#comment-1309</link>

		<dc:creator><![CDATA[Sergey Bastrakov]]></dc:creator>
		<pubDate>Tue, 18 Sep 2018 07:31:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=4275#comment-1309</guid>

					<description><![CDATA[I think in case a code uses vectors of owning raw pointers, it probably does that in a lot of places. Then it is maybe better to have routines to convert it to vectors of unique pointers and back, and use implementations for unique pointers inside. So removing from a vector of raw pointers becomes a conversion + erase_if + conversion back. Doing it just to call remove once is probably not worth it (but I think worth mentioning as a possible way), but such conversions could be useful in other cases as well, and to merge C++11-aware code with legacy one.]]></description>
			<content:encoded><![CDATA[<p>I think in case a code uses vectors of owning raw pointers, it probably does that in a lot of places. Then it is maybe better to have routines to convert it to vectors of unique pointers and back, and use implementations for unique pointers inside. So removing from a vector of raw pointers becomes a conversion + erase_if + conversion back. Doing it just to call remove once is probably not worth it (but I think worth mentioning as a possible way), but such conversions could be useful in other cases as well, and to merge C++11-aware code with legacy one.</p>
]]></content:encoded>
		
			</item>
	</channel>
</rss>
