<?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[for_each mit memberfunktionen]]></title><description><![CDATA[<p>Hallo zusamms,<br />
ich habe (hatte) folgendes Problem:<br />
Ich würde gerne alle Elemente eines STL-Vektors hintereinander als Argument von eine Methode einer Klasse übergeben, also ein Äquivalent für:</p>
<pre><code class="language-cpp">myclass *myobj;
vector&lt;int&gt; a;

// void myclass::doit(int wert)
myobj-&gt;doit(a[0]);
myobj-&gt;doit(a[1]);
myobj-&gt;doit(a[2]);
// usw...
</code></pre>
<p>Hier bietet sich natürlich ein for_each an, jetzt muss ich nur noch die Methode doit der Klasse (*myobj) in ein &quot;unäres Funktionsobjekt&quot; (heisst das so?) umwandeln, die Lösung, die ich gefunden habe ist:</p>
<pre><code class="language-cpp">myclass *myobj;
vector&lt;int&gt; a;

for_each(a.begin(),a.end(), bind1st(mem_fun(&amp;myclass::doit)), myobj));
</code></pre>
<p>Gibt es eine andere Möglichkeit, als das umständliche</p>
<pre><code>bind1st(mem_fun(&amp;myclass::doit)), myobj))
</code></pre>
<p>Konstrukt?<br />
LG, J.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/88245/for_each-mit-memberfunktionen</link><generator>RSS for Node</generator><lastBuildDate>Sun, 02 Aug 2026 17:30:47 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/88245.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 07 Oct 2004 18:36:59 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to for_each mit memberfunktionen on Thu, 07 Oct 2004 18:36:59 GMT]]></title><description><![CDATA[<p>Hallo zusamms,<br />
ich habe (hatte) folgendes Problem:<br />
Ich würde gerne alle Elemente eines STL-Vektors hintereinander als Argument von eine Methode einer Klasse übergeben, also ein Äquivalent für:</p>
<pre><code class="language-cpp">myclass *myobj;
vector&lt;int&gt; a;

// void myclass::doit(int wert)
myobj-&gt;doit(a[0]);
myobj-&gt;doit(a[1]);
myobj-&gt;doit(a[2]);
// usw...
</code></pre>
<p>Hier bietet sich natürlich ein for_each an, jetzt muss ich nur noch die Methode doit der Klasse (*myobj) in ein &quot;unäres Funktionsobjekt&quot; (heisst das so?) umwandeln, die Lösung, die ich gefunden habe ist:</p>
<pre><code class="language-cpp">myclass *myobj;
vector&lt;int&gt; a;

for_each(a.begin(),a.end(), bind1st(mem_fun(&amp;myclass::doit)), myobj));
</code></pre>
<p>Gibt es eine andere Möglichkeit, als das umständliche</p>
<pre><code>bind1st(mem_fun(&amp;myclass::doit)), myobj))
</code></pre>
<p>Konstrukt?<br />
LG, J.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/623516</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/623516</guid><dc:creator><![CDATA[JoeX]]></dc:creator><pubDate>Thu, 07 Oct 2004 18:36:59 GMT</pubDate></item><item><title><![CDATA[Reply to for_each mit memberfunktionen on Thu, 07 Oct 2004 18:56:50 GMT]]></title><description><![CDATA[<p>Wieso nicht einfach</p>
<pre><code class="language-cpp">for (vector&lt;int&gt;::iterator it = a.begin(); it != a.end(); ++it)
    myobj-&gt;doit(*it);
</code></pre>
<p>?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/623533</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/623533</guid><dc:creator><![CDATA[groovemaster]]></dc:creator><pubDate>Thu, 07 Oct 2004 18:56:50 GMT</pubDate></item><item><title><![CDATA[Reply to for_each mit memberfunktionen on Thu, 07 Oct 2004 19:32:46 GMT]]></title><description><![CDATA[<p>mit boost::bind kannst du das schneller machen</p>
<pre><code class="language-cpp">for_each(v.begin(),v.end(),boost::bind(&amp;myclass::doit,myobj));
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/623574</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/623574</guid><dc:creator><![CDATA[rüdiger]]></dc:creator><pubDate>Thu, 07 Oct 2004 19:32:46 GMT</pubDate></item><item><title><![CDATA[Reply to for_each mit memberfunktionen on Thu, 07 Oct 2004 21:51:33 GMT]]></title><description><![CDATA[<p>Du meinst hoffentlich</p>
<pre><code class="language-cpp">std::for_each(v.begin(), v.end(), boost::bind(&amp;myclass::doit, myobj, _1));
</code></pre>
<p>oder? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/623679</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/623679</guid><dc:creator><![CDATA[0xdeadbeef]]></dc:creator><pubDate>Thu, 07 Oct 2004 21:51:33 GMT</pubDate></item><item><title><![CDATA[Reply to for_each mit memberfunktionen on Fri, 08 Oct 2004 06:44:22 GMT]]></title><description><![CDATA[<p>Gerade in Hinblick auf groovemasters Posting: Ist die Verwendung eines STL-Algorithmus wirklich signifikant schneller als das &quot;Ausschreiben&quot; einer normalen for-Schleife? Welchen Nutzen hat erstere noch, außer, daß sie eine kürzere Schreibweise hat?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/623748</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/623748</guid><dc:creator><![CDATA[Cocaine]]></dc:creator><pubDate>Fri, 08 Oct 2004 06:44:22 GMT</pubDate></item><item><title><![CDATA[Reply to for_each mit memberfunktionen on Fri, 08 Oct 2004 09:48:46 GMT]]></title><description><![CDATA[<p>dass du dich nicht um das kümmern musst, was for_each für dich erledigt?<br />
for_each ist fehlerfrei, schon allein deshalb sollte mans benutzen, bevor man einen schusselfehler einbaut <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/623897</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/623897</guid><dc:creator><![CDATA[otze]]></dc:creator><pubDate>Fri, 08 Oct 2004 09:48:46 GMT</pubDate></item><item><title><![CDATA[Reply to for_each mit memberfunktionen on Fri, 08 Oct 2004 10:04:32 GMT]]></title><description><![CDATA[<blockquote>
<p>dass du dich nicht um das kümmern musst, was for_each für dich erledigt?</p>
</blockquote>
<p>Das ist ja auch so höllisch kompliziert.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/623910</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/623910</guid><dc:creator><![CDATA[Bashar]]></dc:creator><pubDate>Fri, 08 Oct 2004 10:04:32 GMT</pubDate></item><item><title><![CDATA[Reply to for_each mit memberfunktionen on Fri, 08 Oct 2004 10:35:48 GMT]]></title><description><![CDATA[<p>Cocaine: Für oft wiederkehrende Sachen ist es ganz hübsch, aber gerade bei speziellen Sachen die uU nur einmal im Programm vorkommen schreibt man sich ne Schleife statt einen extra-Functor zu bauen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/623934</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/623934</guid><dc:creator><![CDATA[Walli]]></dc:creator><pubDate>Fri, 08 Oct 2004 10:35:48 GMT</pubDate></item><item><title><![CDATA[Reply to for_each mit memberfunktionen on Fri, 08 Oct 2004 10:39:29 GMT]]></title><description><![CDATA[<p>otze schrieb:</p>
<blockquote>
<p>dass du dich nicht um das kümmern musst, was for_each für dich erledigt?<br />
for_each ist fehlerfrei, schon allein deshalb sollte mans benutzen, bevor man einen schusselfehler einbaut <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>
</blockquote>
<p>Ich find die zweizeilige for-Schleife zunächst mal leichter nachzuvollziehen, als den UnaryFunction-Kram. Klar gewöhnt man sich dran, aber das ist doch nur sinnvoll, wenn man auch was von hat, oder nicht?</p>
<p><sub>edit</sub><br />
@Mastah: Ok, hübsch ist es - zumal man sofort an for_each, find, transform etc. erkennt, was Sache ist und nicht erst die for-Schleife durchgucken muß. Das kann ein Kommentar allerdings auch...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/623937</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/623937</guid><dc:creator><![CDATA[Cocaine]]></dc:creator><pubDate>Fri, 08 Oct 2004 10:39:29 GMT</pubDate></item><item><title><![CDATA[Reply to for_each mit memberfunktionen on Mon, 11 Oct 2004 10:45:38 GMT]]></title><description><![CDATA[<blockquote>
<p>Cocaine: Für oft wiederkehrende Sachen ist es ganz hübsch, aber gerade bei speziellen Sachen die uU nur einmal im Programm vorkommen schreibt man sich ne Schleife statt einen extra-Functor zu bauen.</p>
</blockquote>
<p>Imo aber nicht deshalb, weil es der bessere Weg ist sondern weil es C++ hier einem unnötig schwer macht.</p>
<p>Ich bevorzuge in solchen Situationen BOOST_FOREACH:</p>
<pre><code class="language-cpp">BOOST_FOREACH(int i, vec)
{
    myObj.doIt(i);   
}
</code></pre>
<p>Letztlich fände ich ein eingebautes for_each oder von mir aus auch ein Block-Konstrukt zusammen mit Lambda-Ausdrücken schöner.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/625860</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/625860</guid><dc:creator><![CDATA[HumeSikkins]]></dc:creator><pubDate>Mon, 11 Oct 2004 10:45:38 GMT</pubDate></item></channel></rss>