<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Elemente einer std::map anhand eines Prädikats löschen]]></title><description><![CDATA[<p>Hi,<br />
ich will bestimmte Elemente einer <code>std::map</code> anhand eines Prädikats löschen. Allerdings habe ich mit dem eigentlichen löschen ein Problem. Jedes <code>erase</code> könnte ja den Suchbaum neu ausbalancieren. Wenn ich nun mit einer for-Schleife und dem begin-Iterator über alle Elemente gehe, wird doch der Iterator, den mir <code>begin(map)</code> geliefert hat, ungültig, sobald ich 1x <code>erase</code> mache, oder? Brauch ich dann immer wieder einen neuen begin-Iterator?</p>
<p>Hier mal ein Beispiel. Ich zähle, wie oft ein Buchstabe in einem Satz vorkommt. Alle Buchstaben, die nur 1x vorkommen, will ich dann löschen:</p>
<pre><code>#include &lt;cctype&gt;
#include &lt;iterator&gt;
#include &lt;map&gt;
#include &lt;string&gt;
using namespace std;

struct map_erase_predicate
{
	unsigned min;
	map_erase_predicate( unsigned min ) : min(min) {}
	bool operator()    ( const pair&lt;char,unsigned&gt;&amp; p ) const { return p.second&lt;min; }
};

void map_erase_elements( map&lt;char,unsigned&gt;&amp; m, const map_erase_predicate&amp; p )
{
	for(auto it=begin(m); it!=end(m); ++it) // Wie muss die for-Schleife aussehen?
	{
		if( p(*it) )
		{
			m.erase( it );
		}
	}
}

int main()
{
	string sentence = &quot;stanleys expeditionszug quer durch afrika wird von jedermann bewundert&quot;;

	map&lt;char,unsigned&gt; count_letters;

	for(auto c : sentence)
	{
		if( isalpha(c) )
			++count_letters[c];
	}

	map_erase_elements( count_letters, map_erase_predicate(2) );
}
</code></pre>
<p>Die for-Schleife funktioniert so ja nicht. Wie müsste sie aussehen?</p>
<p>Danke im Voraus.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/318687/elemente-einer-std-map-anhand-eines-prädikats-löschen</link><generator>RSS for Node</generator><lastBuildDate>Mon, 27 Jul 2026 02:34:59 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/318687.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 22 Jul 2013 18:14:33 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Elemente einer std::map anhand eines Prädikats löschen on Mon, 22 Jul 2013 18:14:33 GMT]]></title><description><![CDATA[<p>Hi,<br />
ich will bestimmte Elemente einer <code>std::map</code> anhand eines Prädikats löschen. Allerdings habe ich mit dem eigentlichen löschen ein Problem. Jedes <code>erase</code> könnte ja den Suchbaum neu ausbalancieren. Wenn ich nun mit einer for-Schleife und dem begin-Iterator über alle Elemente gehe, wird doch der Iterator, den mir <code>begin(map)</code> geliefert hat, ungültig, sobald ich 1x <code>erase</code> mache, oder? Brauch ich dann immer wieder einen neuen begin-Iterator?</p>
<p>Hier mal ein Beispiel. Ich zähle, wie oft ein Buchstabe in einem Satz vorkommt. Alle Buchstaben, die nur 1x vorkommen, will ich dann löschen:</p>
<pre><code>#include &lt;cctype&gt;
#include &lt;iterator&gt;
#include &lt;map&gt;
#include &lt;string&gt;
using namespace std;

struct map_erase_predicate
{
	unsigned min;
	map_erase_predicate( unsigned min ) : min(min) {}
	bool operator()    ( const pair&lt;char,unsigned&gt;&amp; p ) const { return p.second&lt;min; }
};

void map_erase_elements( map&lt;char,unsigned&gt;&amp; m, const map_erase_predicate&amp; p )
{
	for(auto it=begin(m); it!=end(m); ++it) // Wie muss die for-Schleife aussehen?
	{
		if( p(*it) )
		{
			m.erase( it );
		}
	}
}

int main()
{
	string sentence = &quot;stanleys expeditionszug quer durch afrika wird von jedermann bewundert&quot;;

	map&lt;char,unsigned&gt; count_letters;

	for(auto c : sentence)
	{
		if( isalpha(c) )
			++count_letters[c];
	}

	map_erase_elements( count_letters, map_erase_predicate(2) );
}
</code></pre>
<p>Die for-Schleife funktioniert so ja nicht. Wie müsste sie aussehen?</p>
<p>Danke im Voraus.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2340483</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2340483</guid><dc:creator><![CDATA[out]]></dc:creator><pubDate>Mon, 22 Jul 2013 18:14:33 GMT</pubDate></item><item><title><![CDATA[Reply to Elemente einer std::map anhand eines Prädikats löschen on Mon, 22 Jul 2013 18:42:29 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>s. <a href="http://stackoverflow.com/questions/263945/what-happens-if-you-call-erase-on-a-map-element-while-iterating-from-begin-to" rel="nofollow">http://stackoverflow.com/questions/263945/what-happens-if-you-call-erase-on-a-map-element-while-iterating-from-begin-to</a></p>
<p>Durch den erase-Aufruf wird der Iterator ungültig, und daher muß man vorher inkrementieren, um einen Iterator auf das nächste Element zu erhalten.</p>
<p>Aber ab C++11 liefert erase einen Iterator zurück, s. <a href="http://www.cplusplus.com/reference/map/map/erase/" rel="nofollow">http://www.cplusplus.com/reference/map/map/erase/</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2340492</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2340492</guid><dc:creator><![CDATA[Th69]]></dc:creator><pubDate>Mon, 22 Jul 2013 18:42:29 GMT</pubDate></item><item><title><![CDATA[Reply to Elemente einer std::map anhand eines Prädikats löschen on Mon, 22 Jul 2013 18:43:02 GMT]]></title><description><![CDATA[<pre><code>for (auto it = m.begin(); it != m.end(); ) // so muss die for-Schleife aussehen 
    if (p(*it))
        m.erase(it++);
    else
        it++;
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2340493</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2340493</guid><dc:creator><![CDATA[nwp3]]></dc:creator><pubDate>Mon, 22 Jul 2013 18:43:02 GMT</pubDate></item><item><title><![CDATA[Reply to Elemente einer std::map anhand eines Prädikats löschen on Mon, 22 Jul 2013 23:42:02 GMT]]></title><description><![CDATA[<p>Th69 schrieb:</p>
<blockquote>
<p><a href="http://stackoverflow.com/questions/263945/what-happens-if-you-call-erase-on-a-map-element-while-iterating-from-begin-to" rel="nofollow">http://stackoverflow.com/questions/263945/what-happens-if-you-call-erase-on-a-map-element-while-iterating-from-begin-to</a></p>
</blockquote>
<blockquote>
<p>Erasing elements in a map does not invalidate any iterators. (apart from iterators on the element that was deleted.</p>
</blockquote>
<p>Ok, dann hatte ich das irgendwie falsch im Kopf. Danke euch. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2340525</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2340525</guid><dc:creator><![CDATA[out]]></dc:creator><pubDate>Mon, 22 Jul 2013 23:42:02 GMT</pubDate></item><item><title><![CDATA[Reply to Elemente einer std::map anhand eines Prädikats löschen on Tue, 23 Jul 2013 08:03:20 GMT]]></title><description><![CDATA[<p>Wie wärs mit erase und remove_if?</p>
<pre><code>m.erase(remove_if(begin(m), end(m), PREDICATE), end(m));
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2340558</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2340558</guid><dc:creator><![CDATA[Skym0sh0]]></dc:creator><pubDate>Tue, 23 Jul 2013 08:03:20 GMT</pubDate></item><item><title><![CDATA[Reply to Elemente einer std::map anhand eines Prädikats löschen on Tue, 23 Jul 2013 08:34:40 GMT]]></title><description><![CDATA[<p>Skym0sh0 schrieb:</p>
<blockquote>
<p>Wie wärs mit erase und remove_if?</p>
<pre><code>m.erase(remove_if(begin(m), end(m), PREDICATE), end(m));
</code></pre>
</blockquote>
<p><code>std::remove_if</code> geht nicht mit assoziativen Containern.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2340563</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2340563</guid><dc:creator><![CDATA[out]]></dc:creator><pubDate>Tue, 23 Jul 2013 08:34:40 GMT</pubDate></item></channel></rss>