<?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[Prädikat für die Funktion remove_copy_if negieren]]></title><description><![CDATA[<p>Hallo zusammen,</p>
<p>ich möchte für die Funktion <strong>remove_copy_if</strong> aus dem Namensraum std ein Prädikat negieren. Wie ich recht schnell festgestellt hab, reicht ein einfaches '!' nicht.</p>
<p>Somit habe ich folgendes gebastelt bzw. aus der Literatur entnommen:</p>
<p>Prädikat:</p>
<pre><code class="language-cpp">bool isPrim(unsigned int z)
{
	unsigned int stop = sqrt(static_cast&lt;double&gt; (z));

	for (unsigned int i = 2; i &lt;= stop; i++)
	{
		if (z % i == 0)
			return false;
	}
	return true;
}
</code></pre>
<p><strong>Negator:</strong></p>
<pre><code class="language-cpp">template&lt;typename Predicate&gt;
UnaryNegate&lt;Predicate&gt; not1(const Predicate &amp;pred)
{
	return UnaryNegate&lt;Predicate&gt; (pred);
}
</code></pre>
<p><strong>Klasse Unary Negate</strong></p>
<pre><code class="language-cpp">template&lt;typename Predicate&gt;
class UnaryNegate: public unary_function&lt;typename Predicate::argument_type,
		bool&gt;
{
	public:
		explicit UnaryNegate(const Predicate &amp;pre) :
			p(pre)
		{

		}
		virtual ~UnaryNegate();

		bool operator()(const typename Predicate::argument_type&amp; x) const
		{
			return !p(x);
		}

	private:
		Predicate p;
};
</code></pre>
<p>Und zusammen wie folgt angewandt:</p>
<pre><code class="language-cpp">unsigned int numbers[] = { 2, 6, 29, 30, 45, 49, 50 };

//Number of integers
int n = sizeof numbers / 4;remove_copy_if(numbers, numbers + n, ostream_iterator&lt;unsigned int&gt; (cout, &quot;,&quot;), not1(isPrim));
</code></pre>
<p>Mein Compiler sagt mir nun folgendes:</p>
<blockquote>
<p>**** Build of configuration Debug for project Standardbibiothek Beispiele ****</p>
<p>make all<br />
Building file: ../src/Kapitel3.cpp<br />
Invoking: GCC C++ Compiler<br />
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF&quot;src/Kapitel3.d&quot; -MT&quot;src/Kapitel3.d&quot; -o&quot;src/Kapitel3.o&quot; &quot;../src/Kapitel3.cpp&quot;<br />
/usr/include/c++/4.0.0/bits/stl_function.h: In instantiation of 'std::unary_negate&lt;bool ()(unsigned int)&gt;':<br />
../src/Kapitel3.cpp:106: instantiated from here<br />
/usr/include/c++/4.0.0/bits/stl_function.h:314: error: 'bool ()(unsigned int)' is not a class, struct, or union type<br />
/usr/include/c++/4.0.0/bits/stl_function.h:322: error: 'bool ()(unsigned int)' is not a class, struct, or union type<br />
/usr/include/c++/4.0.0/bits/stl_function.h:316: error: field 'std::unary_negate&lt;bool ()(unsigned int)&gt;::_M_pred' invalidly declared function type<br />
/usr/include/c++/4.0.0/bits/stl_algo.h: In function '_OutputIterator std::remove_copy_if(_InputIterator, _InputIterator, _OutputIterator, _Predicate) [with _InputIterator = unsigned int*, _OutputIterator = std::ostream_iterator&lt;unsigned int, char, std::char_traits&lt;char&gt; &gt;, _Predicate = std::unary_negate&lt;bool ()(unsigned int)&gt;]':<br />
../src/Kapitel3.cpp:106: instantiated from here<br />
/usr/include/c++/4.0.0/bits/stl_algo.h:1074: error: no match for call to '(std::unary_negate&lt;bool ()(unsigned int)&gt;) (unsigned int&amp;)'<br />
make: *** [src/Kapitel3.o] Error 1</p>
</blockquote>
<p>Mein Problem ist nun, dass ich nicht weiß wie ich das ganze angehen soll bzw. wie man am besten heraus findet, wo der Fehler liegt ohne alles 1 zu 1 abzuschreiben(aus Literatur etc.). Gebt ihr mir einen Tipp?</p>
<p>Viele Grüße<br />
MM</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/246713/prädikat-für-die-funktion-remove_copy_if-negieren</link><generator>RSS for Node</generator><lastBuildDate>Thu, 17 Sep 2026 21:25:03 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/246713.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 31 Jul 2009 14:20:07 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Prädikat für die Funktion remove_copy_if negieren on Fri, 31 Jul 2009 14:20:07 GMT]]></title><description><![CDATA[<p>Hallo zusammen,</p>
<p>ich möchte für die Funktion <strong>remove_copy_if</strong> aus dem Namensraum std ein Prädikat negieren. Wie ich recht schnell festgestellt hab, reicht ein einfaches '!' nicht.</p>
<p>Somit habe ich folgendes gebastelt bzw. aus der Literatur entnommen:</p>
<p>Prädikat:</p>
<pre><code class="language-cpp">bool isPrim(unsigned int z)
{
	unsigned int stop = sqrt(static_cast&lt;double&gt; (z));

	for (unsigned int i = 2; i &lt;= stop; i++)
	{
		if (z % i == 0)
			return false;
	}
	return true;
}
</code></pre>
<p><strong>Negator:</strong></p>
<pre><code class="language-cpp">template&lt;typename Predicate&gt;
UnaryNegate&lt;Predicate&gt; not1(const Predicate &amp;pred)
{
	return UnaryNegate&lt;Predicate&gt; (pred);
}
</code></pre>
<p><strong>Klasse Unary Negate</strong></p>
<pre><code class="language-cpp">template&lt;typename Predicate&gt;
class UnaryNegate: public unary_function&lt;typename Predicate::argument_type,
		bool&gt;
{
	public:
		explicit UnaryNegate(const Predicate &amp;pre) :
			p(pre)
		{

		}
		virtual ~UnaryNegate();

		bool operator()(const typename Predicate::argument_type&amp; x) const
		{
			return !p(x);
		}

	private:
		Predicate p;
};
</code></pre>
<p>Und zusammen wie folgt angewandt:</p>
<pre><code class="language-cpp">unsigned int numbers[] = { 2, 6, 29, 30, 45, 49, 50 };

//Number of integers
int n = sizeof numbers / 4;remove_copy_if(numbers, numbers + n, ostream_iterator&lt;unsigned int&gt; (cout, &quot;,&quot;), not1(isPrim));
</code></pre>
<p>Mein Compiler sagt mir nun folgendes:</p>
<blockquote>
<p>**** Build of configuration Debug for project Standardbibiothek Beispiele ****</p>
<p>make all<br />
Building file: ../src/Kapitel3.cpp<br />
Invoking: GCC C++ Compiler<br />
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF&quot;src/Kapitel3.d&quot; -MT&quot;src/Kapitel3.d&quot; -o&quot;src/Kapitel3.o&quot; &quot;../src/Kapitel3.cpp&quot;<br />
/usr/include/c++/4.0.0/bits/stl_function.h: In instantiation of 'std::unary_negate&lt;bool ()(unsigned int)&gt;':<br />
../src/Kapitel3.cpp:106: instantiated from here<br />
/usr/include/c++/4.0.0/bits/stl_function.h:314: error: 'bool ()(unsigned int)' is not a class, struct, or union type<br />
/usr/include/c++/4.0.0/bits/stl_function.h:322: error: 'bool ()(unsigned int)' is not a class, struct, or union type<br />
/usr/include/c++/4.0.0/bits/stl_function.h:316: error: field 'std::unary_negate&lt;bool ()(unsigned int)&gt;::_M_pred' invalidly declared function type<br />
/usr/include/c++/4.0.0/bits/stl_algo.h: In function '_OutputIterator std::remove_copy_if(_InputIterator, _InputIterator, _OutputIterator, _Predicate) [with _InputIterator = unsigned int*, _OutputIterator = std::ostream_iterator&lt;unsigned int, char, std::char_traits&lt;char&gt; &gt;, _Predicate = std::unary_negate&lt;bool ()(unsigned int)&gt;]':<br />
../src/Kapitel3.cpp:106: instantiated from here<br />
/usr/include/c++/4.0.0/bits/stl_algo.h:1074: error: no match for call to '(std::unary_negate&lt;bool ()(unsigned int)&gt;) (unsigned int&amp;)'<br />
make: *** [src/Kapitel3.o] Error 1</p>
</blockquote>
<p>Mein Problem ist nun, dass ich nicht weiß wie ich das ganze angehen soll bzw. wie man am besten heraus findet, wo der Fehler liegt ohne alles 1 zu 1 abzuschreiben(aus Literatur etc.). Gebt ihr mir einen Tipp?</p>
<p>Viele Grüße<br />
MM</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1753023</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1753023</guid><dc:creator><![CDATA[MrMilk]]></dc:creator><pubDate>Fri, 31 Jul 2009 14:20:07 GMT</pubDate></item><item><title><![CDATA[Reply to Prädikat für die Funktion remove_copy_if negieren on Fri, 31 Jul 2009 14:27:43 GMT]]></title><description><![CDATA[<p>du brauchst dir dein negations-Prädikat nicht selber zu schreiben, das gibts in der Standardbibliothek schon, im header &lt;functional&gt;, und es heißt std::not1. Das ist auch das was dein Compiler da benutzt.<br />
Versuch mal, statt isPrim direkt zu übergeben, einen Pointer darauf zu übergeben:</p>
<pre><code class="language-cpp">remove_copy_if(numbers, 
               numbers + n, 
               ostream_iterator&lt;unsigned int&gt; (cout, &quot;,&quot;), 
               not1(&amp;isPrim));  // &lt;-!!!
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1753030</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1753030</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Fri, 31 Jul 2009 14:27:43 GMT</pubDate></item><item><title><![CDATA[Reply to Prädikat für die Funktion remove_copy_if negieren on Fri, 31 Jul 2009 14:36:50 GMT]]></title><description><![CDATA[<p>Der Fehler ist einfach und blöd. Man darf keine Funktion übergeben, sondern muss aus der Funktion zuerst ein <code>pointer_to_unary_function</code> Objekt erstellen.</p>
<pre><code class="language-cpp">remove_copy_if(numbers, 
               numbers + n, 
               ostream_iterator&lt;unsigned int&gt; (cout, &quot;,&quot;), 
               not1(ptr_fun(isPrim)));
</code></pre>
<p>Wie du siehst, erreicht man die mit<a href="http://www.cplusplus.com/reference/std/functional/ptr_fun/" rel="nofollow"> <code>std::ptr_fun</code> </a>.</p>
<p>Unter anderem deshalb freuen sich alle über <code>Boost.Bind</code> , <code>std::tr1::bind</code> oder dem C++0x <code>std::bind</code> .</p>
<p>Edit: Rechtschreibefehler.</p>
<p>Grüssli <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/1753037</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1753037</guid><dc:creator><![CDATA[Dravere]]></dc:creator><pubDate>Fri, 31 Jul 2009 14:36:50 GMT</pubDate></item><item><title><![CDATA[Reply to Prädikat für die Funktion remove_copy_if negieren on Fri, 31 Jul 2009 16:13:29 GMT]]></title><description><![CDATA[<p>super, vielen Dank <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="🙂"
    /> Klappt nun.</p>
<p>Viele grüße<br />
MM</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1753083</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1753083</guid><dc:creator><![CDATA[MrMilk]]></dc:creator><pubDate>Fri, 31 Jul 2009 16:13:29 GMT</pubDate></item></channel></rss>