<?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] bei containern mit pointer-objekten]]></title><description><![CDATA[<p>ich hatte das schon in einem anderen thread gefragt, aber leider bisher keine Antwort erhalten. Kann man for_each auch bei containern mit pointern anwenden OHNE prädikate zu erstellen?</p>
<pre><code class="language-cpp">class bar{ 
public:
   bar() {} 
   virtual void foo1() = 0; 
   virtual void foo2() { std::cout &lt;&lt; &quot;bar::foo2&quot;  &lt;&lt; std::endl;  } 
}; 

class bar2 : public bar{ 
public:
   bar2() {} 
   void foo1() { std::cout &lt;&lt; &quot;bar2::foo1&quot;  &lt;&lt; std::endl;  } 
   void foo2() { std::cout &lt;&lt; &quot;bar2::foo2&quot;  &lt;&lt; std::endl;  } 
}; 

class bar3 : public bar{ 
public:
   bar3() {} 
   void foo1() { std::cout &lt;&lt; &quot;bar3::foo1&quot;  &lt;&lt; std::endl;  } 
   void foo2() { std::cout &lt;&lt; &quot;bar3::foo2&quot;  &lt;&lt; std::endl;  } 
}; 

int main() 
{ 
   std::vector&lt;bar2&gt; data1(4); 
   std::for_each(data1.begin(), data1.end(), std::mem_fun(&amp;bar::foo1));

   std::vector&lt;bar*&gt; data2; 
   data2.push_back(new bar2); 
   data2.push_back(new bar2); 
   data2.push_back(new bar2); 
   data2.push_back(new bar3); 

   /* for_each =&gt; Algorithm | Iterator */ 
   //wie sähe hier der for_each Befehl aus? einmal für foo1 und einmal für foo2     
   std::for_each( data2.begin(), data2.end(), ??? ); 
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/206716/for_each-bei-containern-mit-pointer-objekten</link><generator>RSS for Node</generator><lastBuildDate>Sat, 10 Oct 2026 14:48:46 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/206716.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 27 Feb 2008 21:41:38 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to [for_each] bei containern mit pointer-objekten on Wed, 27 Feb 2008 21:41:38 GMT]]></title><description><![CDATA[<p>ich hatte das schon in einem anderen thread gefragt, aber leider bisher keine Antwort erhalten. Kann man for_each auch bei containern mit pointern anwenden OHNE prädikate zu erstellen?</p>
<pre><code class="language-cpp">class bar{ 
public:
   bar() {} 
   virtual void foo1() = 0; 
   virtual void foo2() { std::cout &lt;&lt; &quot;bar::foo2&quot;  &lt;&lt; std::endl;  } 
}; 

class bar2 : public bar{ 
public:
   bar2() {} 
   void foo1() { std::cout &lt;&lt; &quot;bar2::foo1&quot;  &lt;&lt; std::endl;  } 
   void foo2() { std::cout &lt;&lt; &quot;bar2::foo2&quot;  &lt;&lt; std::endl;  } 
}; 

class bar3 : public bar{ 
public:
   bar3() {} 
   void foo1() { std::cout &lt;&lt; &quot;bar3::foo1&quot;  &lt;&lt; std::endl;  } 
   void foo2() { std::cout &lt;&lt; &quot;bar3::foo2&quot;  &lt;&lt; std::endl;  } 
}; 

int main() 
{ 
   std::vector&lt;bar2&gt; data1(4); 
   std::for_each(data1.begin(), data1.end(), std::mem_fun(&amp;bar::foo1));

   std::vector&lt;bar*&gt; data2; 
   data2.push_back(new bar2); 
   data2.push_back(new bar2); 
   data2.push_back(new bar2); 
   data2.push_back(new bar3); 

   /* for_each =&gt; Algorithm | Iterator */ 
   //wie sähe hier der for_each Befehl aus? einmal für foo1 und einmal für foo2     
   std::for_each( data2.begin(), data2.end(), ??? ); 
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1464419</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1464419</guid><dc:creator><![CDATA[muffmolch]]></dc:creator><pubDate>Wed, 27 Feb 2008 21:41:38 GMT</pubDate></item><item><title><![CDATA[Reply to [for_each] bei containern mit pointer-objekten on Wed, 27 Feb 2008 23:15:41 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>wenn du normale Objekte in deinem Container hast, benutzt du mem_fun_ref:</p>
<pre><code class="language-cpp">std::vector&lt;bar2&gt; data1(4);
std::for_each(data1.begin(), data1.end(), std::mem_fun_ref(&amp;bar::foo1));
</code></pre>
<p>Bei Pointern im Container mem_fun:</p>
<pre><code class="language-cpp">std::vector&lt;bar*&gt; data2;
std::for_each( data2.begin(), data2.end(), std::mem_fun(&amp;bar::foo1) );
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1464462</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1464462</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Wed, 27 Feb 2008 23:15:41 GMT</pubDate></item><item><title><![CDATA[Reply to [for_each] bei containern mit pointer-objekten on Thu, 28 Feb 2008 08:37:54 GMT]]></title><description><![CDATA[<p>super danke!<br />
hatte das ähnlich getestet... das mit std::mem_fun_ref hatte ich falsch.</p>
<p>okay. und nun die nächste frage:<br />
wie kann man referenzen übergeben:</p>
<pre><code class="language-cpp">struct bar{
      void foo() {}
      void fooCbv(int&amp; i) {}
      void fooRef(int&amp; i) {}
}
std::vector&lt; bar &gt; data1(10);
int i=0;
for_each(data1.begin(), data1.end(),std::mem_fun_ref(&amp;bar::foo) ); //wuppt
for_each(data1.begin(), data1.end(),std::bind2nd( std::mem_fun_ref(&amp;bar::fooCbv), i) ); //wuppt
for_each(data1.begin(), data1.end(),std::bind2nd( std::mem_fun_ref(&amp;bar::fooRef), i) ); // ERROR
</code></pre>
<p>ERROR:</p>
<blockquote>
<p>functional(312) : warning C4181: qualifier applied to reference type<br />
with _Fn2=std::mem_fun1_ref_t&lt;void,bar,int &amp;&gt;</p>
</blockquote>
<p>Muss man da auch das <strong>i</strong> mit irgend einem Funktional binden?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1464531</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1464531</guid><dc:creator><![CDATA[muffmolch]]></dc:creator><pubDate>Thu, 28 Feb 2008 08:37:54 GMT</pubDate></item><item><title><![CDATA[Reply to [for_each] bei containern mit pointer-objekten on Thu, 28 Feb 2008 11:37:13 GMT]]></title><description><![CDATA[<p>Hmmm, das müsste auch normal funktionieren, aber irgendwas war da mal mit nem Reference-Problem bei sowas.</p>
<p>Mit boost::bind und boost::mem_fn funktionierts jedenfalls ganz normal und find ich auch so schöner, wenn dir boost zur Verfügung steht, würd ichs benutzen:</p>
<pre><code class="language-cpp">struct bar{
      void foo() {}
      void fooCbv(int i)  {}  // Dein Copy-by-Value hatte auch nen &amp; ;)
      void fooRef(int&amp; i) {}
};

int main()
{
    using namespace boost;

    std::vector&lt; bar &gt; data1(10);
    int i=0;

    for_each(data1.begin(), data1.end(), boost::mem_fn(&amp;bar::foo) );
    for_each(data1.begin(), data1.end(), boost::bind( boost::mem_fn(&amp;bar::fooCbv), _1, i) );
    for_each(data1.begin(), data1.end(), boost::bind( boost::mem_fn(&amp;bar::fooRef), _1, i) );
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1464645</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1464645</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Thu, 28 Feb 2008 11:37:13 GMT</pubDate></item><item><title><![CDATA[Reply to [for_each] bei containern mit pointer-objekten on Thu, 28 Feb 2008 11:41:17 GMT]]></title><description><![CDATA[<p>Erstens: Mem_fn braucht man dann auch nicht mehr wirklich.<br />
Zweitens: Es gibt in der Boost-Bibliothek auch einen Wrapper für Referenzen.<br />
Drittens: Das ganze ist seit TR1 auch ein ISO-Standard.</p>
<pre><code class="language-cpp">struct bar{
      void foo() {}
      void fooCbv(int i)  {}  // Dein Copy-by-Value hatte auch nen &amp; ;)
      void fooRef(int&amp; i) {}
};

int main()
{
    using namespace boost;

    std::vector&lt; bar &gt; data1(10);
    int i=0;

    for_each(data1.begin(), data1.end(), std::tr1::bind( &amp;bar::foo, _1 ) );
    for_each(data1.begin(), data1.end(), std::tr1::bind( &amp;bar::fooCbv, _1, i ) );
    for_each(data1.begin(), data1.end(), std::tr1::bind( &amp;bar::fooRef, _1, std::tr1::ref( i ) ) );
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1464649</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1464649</guid><dc:creator><![CDATA[LordJaxom]]></dc:creator><pubDate>Thu, 28 Feb 2008 11:41:17 GMT</pubDate></item><item><title><![CDATA[Reply to [for_each] bei containern mit pointer-objekten on Thu, 28 Feb 2008 11:54:45 GMT]]></title><description><![CDATA[<p>Das sieht ja noch besser aus. TR1 ist bei mir irgendwie in Vergessenheit geraten und da das tr1::bind ohne nen mem_fn sieht für mich ziemlich ungewöhnlich aus ...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1464662</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1464662</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Thu, 28 Feb 2008 11:54:45 GMT</pubDate></item><item><title><![CDATA[Reply to [for_each] bei containern mit pointer-objekten on Thu, 28 Feb 2008 12:02:48 GMT]]></title><description><![CDATA[<p>KasF schrieb:</p>
<blockquote>
<p>Das sieht ja noch besser aus. TR1 ist bei mir irgendwie in Vergessenheit geraten und da das tr1::bind ohne nen mem_fn sieht für mich ziemlich ungewöhnlich aus ...</p>
</blockquote>
<p>Gut, dass es immer mehrere Wege zum Ziel gibt - für mich hat boost::bind <strong>mit</strong> mem_fn auf den ersten Blick ungewöhnlich ausgesehen, da ich beim Kennenlernen von boost direkt ohne gelernt habe zu arbeiten (nach <a href="http://boost.org/libs/bind/bind.html#with_member_pointers" rel="nofollow">http://boost.org/libs/bind/bind.html#with_member_pointers</a>) <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/1464669</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1464669</guid><dc:creator><![CDATA[LordJaxom]]></dc:creator><pubDate>Thu, 28 Feb 2008 12:02:48 GMT</pubDate></item><item><title><![CDATA[Reply to [for_each] bei containern mit pointer-objekten on Thu, 28 Feb 2008 12:07:45 GMT]]></title><description><![CDATA[<p>danke für die antworten... tr1 gefällt mir auch am besten, aber auf unserem linux cluster bekommen die admins leider gcc4.x nicht zum laufen und der gcc3.4 unterstützt kein tr1. boost verwende ich dort zwar, aber nicht für alle konfigurationen!<br />
aber es hilft mir dennoch weiter</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1464674</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1464674</guid><dc:creator><![CDATA[muffmolch]]></dc:creator><pubDate>Thu, 28 Feb 2008 12:07:45 GMT</pubDate></item></channel></rss>