<?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[[GELÖST] Range-For Loop String komisches verhalten (c++11)]]></title><description><![CDATA[<p>EDIT: Eigene Dummheit, Auflösung siehe Antworten unten.</p>
<p>Wenn ich einen Range-For-Loop (C++11) auf einen String anwende erhalte ich ein für mich komisches verhalten:</p>
<pre><code>bool Tokenizer::operator&gt;&gt;(std::string&amp; line)
{
    // Reads a line into &quot;line&quot;
    std::cerr &lt;&lt; &quot;LINE: &quot; &lt;&lt; line &lt;&lt; std::endl;
    for (char c : line) {
        std::cerr &lt;&lt; &quot;CHAR: &quot; &lt;&lt; c &lt;&lt; std::endl;
        // ...
    }
}
</code></pre>
<p>Ausgabe:</p>
<blockquote>
<p>LINE: 1+5++6++++<br />
CHAR: 1<br />
CHAR: s<br />
CHAR: t<br />
CHAR: d<br />
CHAR: :<br />
CHAR: :<br />
CHAR: s<br />
CHAR: t<br />
CHAR: r<br />
CHAR: i</p>
</blockquote>
<p>ich würde jetzt 1 + 5 ... erwarten.<br />
Wo mache ich meinen Denkfehler?<br />
MfG</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/336645/gelöst-range-for-loop-string-komisches-verhalten-c-11</link><generator>RSS for Node</generator><lastBuildDate>Sun, 19 Apr 2026 00:03:10 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/336645.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 06 Feb 2016 16:32:13 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to [GELÖST] Range-For Loop String komisches verhalten (c++11) on Sat, 06 Feb 2016 17:09:36 GMT]]></title><description><![CDATA[<p>EDIT: Eigene Dummheit, Auflösung siehe Antworten unten.</p>
<p>Wenn ich einen Range-For-Loop (C++11) auf einen String anwende erhalte ich ein für mich komisches verhalten:</p>
<pre><code>bool Tokenizer::operator&gt;&gt;(std::string&amp; line)
{
    // Reads a line into &quot;line&quot;
    std::cerr &lt;&lt; &quot;LINE: &quot; &lt;&lt; line &lt;&lt; std::endl;
    for (char c : line) {
        std::cerr &lt;&lt; &quot;CHAR: &quot; &lt;&lt; c &lt;&lt; std::endl;
        // ...
    }
}
</code></pre>
<p>Ausgabe:</p>
<blockquote>
<p>LINE: 1+5++6++++<br />
CHAR: 1<br />
CHAR: s<br />
CHAR: t<br />
CHAR: d<br />
CHAR: :<br />
CHAR: :<br />
CHAR: s<br />
CHAR: t<br />
CHAR: r<br />
CHAR: i</p>
</blockquote>
<p>ich würde jetzt 1 + 5 ... erwarten.<br />
Wo mache ich meinen Denkfehler?<br />
MfG</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2486089</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2486089</guid><dc:creator><![CDATA[derFer]]></dc:creator><pubDate>Sat, 06 Feb 2016 17:09:36 GMT</pubDate></item><item><title><![CDATA[Reply to [GELÖST] Range-For Loop String komisches verhalten (c++11) on Sat, 06 Feb 2016 16:41:31 GMT]]></title><description><![CDATA[<p>Die Range based for Loop sieht OK aus. Bei mir funktioniert alles wie erwartet. Der Fehler steckt wohl woanders. Verändest du vielleicht <code>line</code> in der for Loop? Du hast da mit <code>// ...</code> das da noch mehr in der Schleife steht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2486092</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2486092</guid><dc:creator><![CDATA[sebi707]]></dc:creator><pubDate>Sat, 06 Feb 2016 16:41:31 GMT</pubDate></item><item><title><![CDATA[Reply to [GELÖST] Range-For Loop String komisches verhalten (c++11) on Sat, 06 Feb 2016 17:07:46 GMT]]></title><description><![CDATA[<p>Ah ok stimmt in einer verschachtelten Funktion hab ich doch wirklich line geändert.<br />
line += andererstring.</p>
<p>Komisch woher dann das std::string kam, weil das tauch nirgends auf (anderer string ist definiert als &quot; &quot; + c).</p>
<p>Danke <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>
<p>Edit: War echt ein dummer Fehler:</p>
<pre><code>last = &quot; &quot; + c;
// ...
line += last;
</code></pre>
<p>Gibt selbst mit -pedantic keine Warnung, ist ja valid nur halt großer Bullshit.<br />
Also line durch tmp ersetzen und + c entweder durch push_back oder std::string(1, c).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2486093</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2486093</guid><dc:creator><![CDATA[derFer]]></dc:creator><pubDate>Sat, 06 Feb 2016 17:07:46 GMT</pubDate></item><item><title><![CDATA[Reply to [GELÖST] Range-For Loop String komisches verhalten (c++11) on Sat, 06 Feb 2016 17:06:03 GMT]]></title><description><![CDATA[<p>derFer schrieb:</p>
<blockquote>
<p>Komisch woher dann das std::string kam, weil das tauch nirgends auf (anderer string ist definiert als &quot; &quot; + c).</p>
</blockquote>
<p>Woher der oben gezeigte String kam oder was? Wenn du an <code>line</code> irgendwas anhängst wird irgendwann der von <code>std::string</code> reservierte Platz nicht mehr ausreichen und es wird ein neuer Speicherbereich reserviert und die alten Iteratoren (welche auch die Range based for Loop zum iterieren nutzt) werden ungültig und man kann dort alles mögliche lesen, was nichts mehr mit dem ursprünglichen String zu tun hat. Vielleicht ist auch dein <code>&quot; &quot; + c</code> nicht das was du erwartest, das hängt davon ab was <code>c</code> für einen Typ hat. Wenn <code>c</code> kein <code>std::string</code> ist dann passiert sicher nicht das was du erhoffst.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2486094</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2486094</guid><dc:creator><![CDATA[sebi707]]></dc:creator><pubDate>Sat, 06 Feb 2016 17:06:03 GMT</pubDate></item><item><title><![CDATA[Reply to [GELÖST] Range-For Loop String komisches verhalten (c++11) on Sat, 06 Feb 2016 17:14:09 GMT]]></title><description><![CDATA[<p>derFer schrieb:</p>
<blockquote>
<p>und + c entweder durch push_back oder std::string(1, c).</p>
</blockquote>
<p>Seit C++14 geht auch:</p>
<pre><code>last = &quot; &quot;s + c;
</code></pre>
<p>Jetzt ist <code>&quot; &quot;s</code> ein <code>std::string</code> und <code>operator+</code> macht was es soll.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2486095</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2486095</guid><dc:creator><![CDATA[sebi707]]></dc:creator><pubDate>Sat, 06 Feb 2016 17:14:09 GMT</pubDate></item><item><title><![CDATA[Reply to [GELÖST] Range-For Loop String komisches verhalten (c++11) on Sat, 06 Feb 2016 18:38:31 GMT]]></title><description><![CDATA[<p>sebi707 schrieb:</p>
<blockquote>
<p>Seit C++14 geht auch:</p>
<pre><code>last = &quot; &quot;s + c;
</code></pre>
<p>Jetzt ist <code>&quot; &quot;s</code> ein <code>std::string</code> und <code>operator+</code> macht was es soll.</p>
</blockquote>
<p>Oh danke, das ist wirklich nützlich <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>
<p>Ich hoffe die Compiler führen schnell C++14 Unterstützung ein und die Distributionen steigen endlich mal um.<br />
(Leider haben viele noch gcc 4.8 das nicht mal C++11 wirklich unterstützt (z.b. regex)).</p>
<p>(selbes gilt übrigens auch für msvc).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2486107</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2486107</guid><dc:creator><![CDATA[derFer]]></dc:creator><pubDate>Sat, 06 Feb 2016 18:38:31 GMT</pubDate></item><item><title><![CDATA[Reply to [GELÖST] Range-For Loop String komisches verhalten (c++11) on Sat, 06 Feb 2016 19:31:23 GMT]]></title><description><![CDATA[<p>derFer schrieb:</p>
<blockquote>
<p>Ich hoffe die Compiler führen schnell C++14 Unterstützung ein</p>
</blockquote>
<p>Die sind schon lange fertig!</p>
<blockquote>
<p>und die Distributionen steigen endlich mal um.</p>
</blockquote>
<p>Dies ist dein Problem. Musst du eben selbst tätig werden, wenn deine Distro es nicht tut.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2486114</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2486114</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Sat, 06 Feb 2016 19:31:23 GMT</pubDate></item><item><title><![CDATA[Reply to [GELÖST] Range-For Loop String komisches verhalten (c++11) on Sat, 06 Feb 2016 21:57:33 GMT]]></title><description><![CDATA[<p>SeppJ schrieb:</p>
<blockquote>
<p>Die sind schon lange fertig!</p>
</blockquote>
<p>Naja... <a href="https://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.2014" rel="nofollow">https://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.2014</a><br />
Bei msvc ist es noch schlimmer da fehlen selbst in der 2015 version c++11 features.</p>
<p>SeppJ schrieb:</p>
<blockquote>
<p>Dies ist dein Problem. Musst du eben selbst tätig werden, wenn deine Distro es nicht tut.</p>
</blockquote>
<p>Ja das ist schon klar, ich selber habe da keine Probleme, zur Not baue ich mir den GCC selber. Viel mehr sind die Nutzer das Problem auf deren System habe ich ja keinen Zugriff <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>
<p>Aber das ist ein Anderes Thema, wir kommen schon in den OT rein.<br />
Danke für die Antworten und noch ein schönes WE,</p>
<p>Fer</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2486137</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2486137</guid><dc:creator><![CDATA[derFer]]></dc:creator><pubDate>Sat, 06 Feb 2016 21:57:33 GMT</pubDate></item></channel></rss>