<?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[Schleife nach Funktionsaufruf weiterlaufen lassen?]]></title><description><![CDATA[<p>Hallo,</p>
<p>angenommen ich hab so einen Code:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;vector&gt;

void do_something(std::vector&lt;int&gt; &amp;v)
{
	v.push_back(8);
}

int main()
{
	std::vector&lt;int&gt; v = { 1, 2, 3, 4, 5 };

	for (auto it = v.begin(); it != v.end(); ++it) {
		std::cout &lt;&lt; *it &lt;&lt; ' ';
		do_something(v);
	}
	std::cout &lt;&lt; '\n';
}
</code></pre>
<p>Wie kann ich sicherstellen dass die Schleife am aktuellen Element weiter läuft? Also Ausgabe stelle ich mir so vor:</p>
<p>1 2 3 4 5 8 8 8 .... (Endlos-Schleife)</p>
<p>Zwischenspeichern kann ich den iterator ja nicht, da der ja invalidiert wird, daher führt der Code so auch zu einem Laufzeitfehler...</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/328765/schleife-nach-funktionsaufruf-weiterlaufen-lassen</link><generator>RSS for Node</generator><lastBuildDate>Tue, 07 Jul 2026 07:47:41 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/328765.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 26 Oct 2014 10:27:13 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Schleife nach Funktionsaufruf weiterlaufen lassen? on Sun, 26 Oct 2014 10:27:51 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>angenommen ich hab so einen Code:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;vector&gt;

void do_something(std::vector&lt;int&gt; &amp;v)
{
	v.push_back(8);
}

int main()
{
	std::vector&lt;int&gt; v = { 1, 2, 3, 4, 5 };

	for (auto it = v.begin(); it != v.end(); ++it) {
		std::cout &lt;&lt; *it &lt;&lt; ' ';
		do_something(v);
	}
	std::cout &lt;&lt; '\n';
}
</code></pre>
<p>Wie kann ich sicherstellen dass die Schleife am aktuellen Element weiter läuft? Also Ausgabe stelle ich mir so vor:</p>
<p>1 2 3 4 5 8 8 8 .... (Endlos-Schleife)</p>
<p>Zwischenspeichern kann ich den iterator ja nicht, da der ja invalidiert wird, daher führt der Code so auch zu einem Laufzeitfehler...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2424067</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2424067</guid><dc:creator><![CDATA[happystudent]]></dc:creator><pubDate>Sun, 26 Oct 2014 10:27:51 GMT</pubDate></item><item><title><![CDATA[Reply to Schleife nach Funktionsaufruf weiterlaufen lassen? on Sun, 26 Oct 2014 10:32:27 GMT]]></title><description><![CDATA[<p>'nen Index nehmen?</p>
<p>Aber: Warum?! <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/2424068</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2424068</guid><dc:creator><![CDATA[Furble Wurble]]></dc:creator><pubDate>Sun, 26 Oct 2014 10:32:27 GMT</pubDate></item><item><title><![CDATA[Reply to Schleife nach Funktionsaufruf weiterlaufen lassen? on Sun, 26 Oct 2014 10:36:33 GMT]]></title><description><![CDATA[<pre><code>for (auto it = v.begin(); ;) {
   if (it != v.end()) ++it;
   //...
}
</code></pre>
<p>Nicht getestet.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2424069</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2424069</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Sun, 26 Oct 2014 10:36:33 GMT</pubDate></item><item><title><![CDATA[Reply to Schleife nach Funktionsaufruf weiterlaufen lassen? on Sun, 26 Oct 2014 10:41:50 GMT]]></title><description><![CDATA[<p>Furble Wurble schrieb:</p>
<blockquote>
<p>'nen Index nehmen?</p>
<p>Aber: Warum?! <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>
</blockquote>
<p>Also, es sollte halt für beliebige Container gehen, also etwa so (daher will ich keinen Index verwenden):</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;vector&gt;

template &lt;typename container, typename T&gt; // Spezialisierbar für andere Container
void add_to_container(container &amp;c, T value) { c.push_back(value); } 

void do_something(std::vector&lt;int&gt; &amp;v)
{
    add_to_container(v, 8);
}

int main()
{
    std::vector&lt;int&gt; v = { 1, 2, 3, 4, 5 };

    for (auto it = v.begin(); it != v.end(); ++it) {
        std::cout &lt;&lt; *it &lt;&lt; ' ';
        do_something(v);
    }
    std::cout &lt;&lt; '\n';
}
</code></pre>
<p>Geht das also irgendwie auch mit Iteratoren?</p>
<p>Edit:</p>
<p>IBV schrieb:</p>
<blockquote>
<pre><code>for (auto it = v.begin(); ;) {
   if (it != v.end()) ++it;
   //...
}
</code></pre>
<p>Nicht getestet.</p>
</blockquote>
<p>Ja, also das geht nicht weil der Iterator ja invalidiert wird. Daher bringt das jetzt auch nichts, das Inkrement aus dem Schleifenkopf rauszuziehen...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2424070</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2424070</guid><dc:creator><![CDATA[happystudent]]></dc:creator><pubDate>Sun, 26 Oct 2014 10:41:50 GMT</pubDate></item><item><title><![CDATA[Reply to Schleife nach Funktionsaufruf weiterlaufen lassen? on Sun, 26 Oct 2014 11:07:17 GMT]]></title><description><![CDATA[<p>Wenn kein Speicher angefordert wird, bleiben die Iteratoren gültig. Also:</p>
<pre><code class="language-cpp">v.reserve(2*v.size());
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2424074</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2424074</guid><dc:creator><![CDATA[manni66]]></dc:creator><pubDate>Sun, 26 Oct 2014 11:07:17 GMT</pubDate></item><item><title><![CDATA[Reply to Schleife nach Funktionsaufruf weiterlaufen lassen? on Sun, 26 Oct 2014 11:09:49 GMT]]></title><description><![CDATA[<p>happystudent schrieb:</p>
<blockquote>
<p>Furble Wurble schrieb:</p>
<blockquote>
<p>'nen Index nehmen?</p>
<p>Aber: Warum?! <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>
</blockquote>
<p>Also, es sollte halt für beliebige Container gehen, also etwa so (daher will ich keinen Index verwenden):</p>
</blockquote>
<p>Trotzdem: Warum? Warum brauchst du das? Kannste nicht erst alles adden und dann iterieren?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2424075</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2424075</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Sun, 26 Oct 2014 11:09:49 GMT</pubDate></item><item><title><![CDATA[Reply to Schleife nach Funktionsaufruf weiterlaufen lassen? on Sun, 26 Oct 2014 12:05:22 GMT]]></title><description><![CDATA[<p>Nathan schrieb:</p>
<blockquote>
<p>Trotzdem: Warum? Warum brauchst du das? Kannste nicht erst alles adden und dann iterieren?</p>
</blockquote>
<p>Hm, werd vielleicht nochmal genauer über das Design nachdenken, vermutlich schon. Falls doch nicht, schreib ich hier nochmal.</p>
<p>Auf jeden Fall Danke für die Kommentare.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2424093</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2424093</guid><dc:creator><![CDATA[happystudent]]></dc:creator><pubDate>Sun, 26 Oct 2014 12:05:22 GMT</pubDate></item><item><title><![CDATA[Reply to Schleife nach Funktionsaufruf weiterlaufen lassen? on Mon, 27 Oct 2014 05:42:16 GMT]]></title><description><![CDATA[<p>Ich hoffe ich habe dich richtig verstanden, dass du in deinem Vektor die 8 als letztes Element schieben möchtest und das letzte Element (die <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f60e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--smiling_face_with_sunglasses"
      title="8)"
      alt="😎"
    /> dann in einer Endlosschleife ausgeben möchtest.</p>
<p>Hier wäre meine Beispiele</p>
<p>Mit der Endlosschleife</p>
<pre><code>#include &lt;iostream&gt; 
#include &lt;vector&gt; 
using namespace std;

void do_something(std::vector&lt;int&gt; &amp;v) 
{ 
    v.push_back(8); 
} 

int main() 
{ 
    std::vector&lt;int&gt; v = { 1, 2, 3, 4, 5 }; 
    for(unsigned i = 0; true; ++i){
       if(i == 0){
         for(auto v1: v){
           cout &lt;&lt; v1 &lt;&lt; &quot; &quot;;
         }
       }
    do_something(v);    vector&lt;int&gt;::iterator vLast = v.end() -1;
    cout &lt;&lt; *vLast &lt;&lt; &quot; &quot;;

    }
    std::cout &lt;&lt; '\n'; 
}
</code></pre>
<p>ohne die Endlosschleife</p>
<pre><code>#include &lt;iostream&gt; 
#include &lt;vector&gt; 
using namespace std;

void do_something(std::vector&lt;int&gt; &amp;v) 
{ 
    v.push_back(8); 
} 

int main() 
{ 
unsigned achtdranhaengen = 2; // verändern nach wunsch 
    std::vector&lt;int&gt; v = { 1, 2, 3, 4, 5 }; 
    for(unsigned i = 0; i &lt; achtdranhaengen; ++i){
       if(i == 0){
         for(auto v1: v){
           cout &lt;&lt; v1 &lt;&lt; &quot; &quot;;
         }
       }
    do_something(v);
    vector&lt;int&gt;::iterator vLast = v.end() -1;
    cout &lt;&lt; *vLast &lt;&lt; &quot; &quot;;

    }
    std::cout &lt;&lt; '\n'; 
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2424225</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2424225</guid><dc:creator><![CDATA[TemplateQ]]></dc:creator><pubDate>Mon, 27 Oct 2014 05:42:16 GMT</pubDate></item></channel></rss>