<?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[Neue Memberfunktion für String]]></title><description><![CDATA[<p>Ich will eine neue Funktion für die Klasse String schreiben. Wie geht das???</p>
<p>P.S. Die Funktion hab ich schon.</p>
<pre><code class="language-cpp">string trim(string str)
{
	while((str[0] == '\n') || (str[0] == ' ') || (str[0] == '\t'))
		str = str.substr(1);

	return str;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/278824/neue-memberfunktion-für-string</link><generator>RSS for Node</generator><lastBuildDate>Mon, 24 Aug 2026 23:17:58 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/278824.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 13 Dec 2010 17:27:35 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Neue Memberfunktion für String on Mon, 13 Dec 2010 17:27:35 GMT]]></title><description><![CDATA[<p>Ich will eine neue Funktion für die Klasse String schreiben. Wie geht das???</p>
<p>P.S. Die Funktion hab ich schon.</p>
<pre><code class="language-cpp">string trim(string str)
{
	while((str[0] == '\n') || (str[0] == ' ') || (str[0] == '\t'))
		str = str.substr(1);

	return str;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1993916</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1993916</guid><dc:creator><![CDATA[Die erinnerung]]></dc:creator><pubDate>Mon, 13 Dec 2010 17:27:35 GMT</pubDate></item><item><title><![CDATA[Reply to Neue Memberfunktion für String on Mon, 13 Dec 2010 17:31:48 GMT]]></title><description><![CDATA[<p>Gar nicht. Welchen Vorteil hättest du bei einer Implementierung als Memberfunktion? Keinen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1993921</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1993921</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Mon, 13 Dec 2010 17:31:48 GMT</pubDate></item><item><title><![CDATA[Reply to Neue Memberfunktion für String on Mon, 13 Dec 2010 17:33:31 GMT]]></title><description><![CDATA[<p>Genau so: als freie Funktion. Memberfunktionen nachträglich hinzufügen wo man sie braucht, wie das z.B. in Ruby geht, kann man in C++ nicht.<br />
Du kannst dir auch mal <a href="http://www.cplusplus.com/reference/std/locale/isspace/" rel="nofollow">std::isspace</a> anschauen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1993924</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1993924</guid><dc:creator><![CDATA[l&#x27;abra d&#x27;or]]></dc:creator><pubDate>Mon, 13 Dec 2010 17:33:31 GMT</pubDate></item><item><title><![CDATA[Reply to Neue Memberfunktion für String on Mon, 13 Dec 2010 17:34:33 GMT]]></title><description><![CDATA[<p>Doch , ich will nicht ständig</p>
<pre><code class="language-cpp">str = trim(str);
</code></pre>
<p>, sondern</p>
<pre><code class="language-cpp">str.trim();
</code></pre>
<p>und außerdem kann ich nicht</p>
<pre><code class="language-cpp">string trim(string&amp; str)
{
    while((str[0] == '\n') || (str[0] == ' ') || (str[0] == '\t'))
        str = str.substr(1);

    return str;
}
</code></pre>
<p>schreiben, da das mit soetwas wie &quot; lol&quot; nicht fertig wird. Und beide Überladen, geht auch nicht, da wenn ich eine Variable verwende, der Compiler sich nicht entscheiden kann!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1993925</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1993925</guid><dc:creator><![CDATA[Die erinnerung]]></dc:creator><pubDate>Mon, 13 Dec 2010 17:34:33 GMT</pubDate></item><item><title><![CDATA[Reply to Neue Memberfunktion für String on Mon, 13 Dec 2010 17:41:16 GMT]]></title><description><![CDATA[<p>Die erinnerung schrieb:</p>
<blockquote>
<p>Doch , ich will nicht ständig</p>
<pre><code class="language-cpp">str = trim(str);
</code></pre>
<p>, sondern</p>
<pre><code class="language-cpp">str.trim();
</code></pre>
</blockquote>
<p>Dann erb private von std::string und mach deine eigene Klasse.</p>
<blockquote>
<p>und außerdem kann ich nicht</p>
<pre><code class="language-cpp">string trim(string&amp; str)
{
    while((str[0] == '\n') || (str[0] == ' ') || (str[0] == '\t'))
        str = str.substr(1);

    return str;
}
</code></pre>
<p>schreiben, da das mit soetwas wie &quot; lol&quot; nicht fertig wird.</p>
</blockquote>
<p>Warum machst du deine Referenz dann nicht const?</p>
<p>Aber hier hast du dann gleich den Nachteil deiner eigenen Klasse. Du könntest nicht <code>&quot; lol&quot;.trim()</code> schreiben und <code>my_string(&quot; lol&quot;).trim()</code> macht auch nicht was du suchst. Selbst wenn du irgendwie std::string deine Funktion als Member hinzufügen könntest, wäre <code>std::string(&quot; lol&quot;).trim()</code> immer noch nicht was du willst.</p>
<p>Hingegen geht <code>std::string ergebnis = trim(&quot; lol&quot;)</code> ganz ohne Probleme.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1993931</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1993931</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Mon, 13 Dec 2010 17:41:16 GMT</pubDate></item><item><title><![CDATA[Reply to Neue Memberfunktion für String on Mon, 13 Dec 2010 17:41:47 GMT]]></title><description><![CDATA[<p>Die erinnerung schrieb:</p>
<blockquote>
<p>Doch , ich will nicht ständig</p>
<pre><code class="language-cpp">str = trim(str);
</code></pre>
<p>, sondern</p>
<pre><code class="language-cpp">str.trim();
</code></pre>
</blockquote>
<p>Geht nicht, das musst du akzeptieren. Aber das wäre ohnehin nicht sinnvoll. In C++ hast du freie Funktionen zur Erweiterung von Funktionalität, dann sieht das so aus:</p>
<pre><code class="language-cpp">trim(str);
</code></pre>
<p>Die erinnerung schrieb:</p>
<blockquote>
<p>und außerdem kann ich nicht schreiben, da das mit soetwas wie &quot; lol&quot; nicht fertig wird.</p>
</blockquote>
<p>Was für eine Begründung. <code>&quot;lol&quot;.trim()</code> geht genauso wenig. Warum überhaupt sollte man temporäre Objekte ändern wollen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1993932</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1993932</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Mon, 13 Dec 2010 17:41:47 GMT</pubDate></item><item><title><![CDATA[Reply to Neue Memberfunktion für String on Mon, 13 Dec 2010 17:45:29 GMT]]></title><description><![CDATA[<p>Ich glaube das Dürfte funktionieren:</p>
<pre><code class="language-cpp">#include &lt;string&gt;
#include &lt;iostream&gt;
using namespace std;

string trim(string&amp; str)
{
    while((str[0] == '\n') || (str[0] == ' ') || (str[0] == '\t'))
        str = str.substr(1);

    return str;
}

string trim(string str)
{
    while((str[0] == '\n') || (str[0] == ' ') || (str[0] == '\t'))
        str = str.substr(1);

    return str;
}

int main()
{
    string str = &quot;   lol&quot;;

    cout &lt;&lt; trim(&quot;   lol&quot;) &lt;&lt; endl &lt;&lt; trim(&amp;str) &lt;&lt; endl &lt;&lt; str &lt;&lt; endl;

    return 0;
}
</code></pre>
<p>Oder was meint ihr?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1993935</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1993935</guid><dc:creator><![CDATA[Die erinnerung]]></dc:creator><pubDate>Mon, 13 Dec 2010 17:45:29 GMT</pubDate></item><item><title><![CDATA[Reply to Neue Memberfunktion für String on Mon, 13 Dec 2010 17:48:37 GMT]]></title><description><![CDATA[<p>Dass es nicht geht, hast du doch in schon selbst festgestellt.<br />
Der Compiler wird dir das gerne noch einmal bestätigen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1993938</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1993938</guid><dc:creator><![CDATA[Athar]]></dc:creator><pubDate>Mon, 13 Dec 2010 17:48:37 GMT</pubDate></item><item><title><![CDATA[Reply to Neue Memberfunktion für String on Mon, 13 Dec 2010 17:50:29 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">string trim(string&amp; str)
</code></pre>
<p>hat eine merkwürdige Semantik. Willst du nicht ein Inplace-Trim und ein Kopier-Trim bereitstellen?</p>
<p>Das würde ich so tun:</p>
<pre><code class="language-cpp">void trim(std::string&amp; str)
{
    ...
}

std::string trim_copy(std::string str)
{
    trim(str);
    return str;    
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1993941</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1993941</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Mon, 13 Dec 2010 17:50:29 GMT</pubDate></item><item><title><![CDATA[Reply to Neue Memberfunktion für String on Mon, 13 Dec 2010 17:51:32 GMT]]></title><description><![CDATA[<p>Die erinnerung schrieb:</p>
<blockquote>
<p>Oder was meint ihr?</p>
</blockquote>
<p>Wir meinen: Nimm NUR die zweite Variante, die erste macht dir dein Programm kaputt...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1993942</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1993942</guid><dc:creator><![CDATA[l&#x27;abra d&#x27;or]]></dc:creator><pubDate>Mon, 13 Dec 2010 17:51:32 GMT</pubDate></item><item><title><![CDATA[Reply to Neue Memberfunktion für String on Mon, 13 Dec 2010 17:58:34 GMT]]></title><description><![CDATA[<p>l'abra d'or schrieb:</p>
<blockquote>
<p>Die erinnerung schrieb:</p>
<blockquote>
<p>Oder was meint ihr?</p>
</blockquote>
<p>Wir meinen: Nimm NUR die zweite Variante, die erste macht dir dein Programm kaputt...</p>
</blockquote>
<p>0k. War nur ne Frage. Ich verwende im Moment eh nur die 2. Version im Programm.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1993952</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1993952</guid><dc:creator><![CDATA[Die erinnerung]]></dc:creator><pubDate>Mon, 13 Dec 2010 17:58:34 GMT</pubDate></item><item><title><![CDATA[Reply to Neue Memberfunktion für String on Mon, 13 Dec 2010 18:26:48 GMT]]></title><description><![CDATA[<p>Dann noch ein paar Tips wie du den Code optimieren kannst:<br />
Kopien sind teuer. Deshalb solltest du das Argument auch per const-Referenz übergeben.<br />
Auch substr() erzeugt eine Kopie -&gt; teuer!<br />
Speicher dir stattdessen einen &quot;size_t currentIndex&quot;, den du immer hochzählst, solange str[currentIndex] ein Space ist. Gibt isspace false zurück, hast du deinen Index für substr. Dann hast du<br />
Übergabe per Referenz (billig) Index inkrementieren (billig) und ein substr() - die teuerste Operation in der Funktion.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1993979</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1993979</guid><dc:creator><![CDATA[l&#x27;abra d&#x27;or]]></dc:creator><pubDate>Mon, 13 Dec 2010 18:26:48 GMT</pubDate></item><item><title><![CDATA[Reply to Neue Memberfunktion für String on Mon, 13 Dec 2010 18:31:14 GMT]]></title><description><![CDATA[<p>Und das ganze als Funktion für nichtFreaks, bitte?!?!?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1993983</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1993983</guid><dc:creator><![CDATA[Die erinnerung]]></dc:creator><pubDate>Mon, 13 Dec 2010 18:31:14 GMT</pubDate></item><item><title><![CDATA[Reply to Neue Memberfunktion für String on Mon, 13 Dec 2010 18:39:37 GMT]]></title><description><![CDATA[<p>l'abra d'or sagte, dass du alle Whitespacezeichen gleichzeitig mit einem einzigen Aufruf von substr wegmachen sollst und dass der Parameter eine konstante Referenz sein sollte, damit keine unnötige Kopie angefertigt wird.<br />
War doch verständlich genug.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1993997</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1993997</guid><dc:creator><![CDATA[Athar]]></dc:creator><pubDate>Mon, 13 Dec 2010 18:39:37 GMT</pubDate></item><item><title><![CDATA[Reply to Neue Memberfunktion für String on Mon, 13 Dec 2010 18:44:49 GMT]]></title><description><![CDATA[<p>Beispielsweise</p>
<pre><code class="language-cpp">std::string trim_copy(std::string const &amp;s) {
  std::string::size_type vorn   = s.find_first_not_of(&quot; \t\n&quot;);

  if(vorn == std::string::npos) return &quot;&quot;;

  std::string::size_type hinten = s.find_last_not_of (&quot; \t\n&quot;);

  return s.substr(vorn, hinten - vorn + 1);
}
</code></pre>
<p>Allerdings gibt's das Ganze von boost schon hübscher (und mit Locale-Unterstützung): <a href="http://www.boost.org/doc/libs/1_45_0/doc/html/string_algo.html" rel="nofollow">http://www.boost.org/doc/libs/1_45_0/doc/html/string_algo.html</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1994003</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1994003</guid><dc:creator><![CDATA[seldon]]></dc:creator><pubDate>Mon, 13 Dec 2010 18:44:49 GMT</pubDate></item><item><title><![CDATA[Reply to Neue Memberfunktion für String on Mon, 13 Dec 2010 20:49:36 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">std::string Trim( std::string &amp; str )
{ 
size_t trim;

	if( (trim = str.find_first_not_of( &quot; \t&quot; )) != string::npos )
		str.erase( 0, trim );
	if( (trim = str.find_last_not_of( &quot; \t&quot; )) != string::npos )
		str.resize( trim+1 );
	return( str );
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1994059</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1994059</guid><dc:creator><![CDATA[EOP]]></dc:creator><pubDate>Mon, 13 Dec 2010 20:49:36 GMT</pubDate></item></channel></rss>