<?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[Doppelte (bzw. mehrfach) Vorkommende Leerzeichen in std::string löschen?]]></title><description><![CDATA[<p>Hallo,<br />
ich suche nach dem effizientesten Weg, aus einem std::string der z.B. so aussieht:</p>
<pre><code>a b  c   d e
</code></pre>
<p>einen std::string zu machen, der so aussieht:</p>
<pre><code>a b c d e
</code></pre>
<p>Ich habe es zwar auch schon geschafft, nur ist meine Lösung so unglaublich langsam, dass ich mir denke, dass das niemals die Beste sein kann.<br />
Bitte um Hilfe.<br />
Danke! <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/topic/261125/doppelte-bzw-mehrfach-vorkommende-leerzeichen-in-std-string-löschen</link><generator>RSS for Node</generator><lastBuildDate>Mon, 07 Sep 2026 16:31:52 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/261125.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 14 Feb 2010 04:53:26 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Doppelte (bzw. mehrfach) Vorkommende Leerzeichen in std::string löschen? on Sun, 14 Feb 2010 04:53:26 GMT]]></title><description><![CDATA[<p>Hallo,<br />
ich suche nach dem effizientesten Weg, aus einem std::string der z.B. so aussieht:</p>
<pre><code>a b  c   d e
</code></pre>
<p>einen std::string zu machen, der so aussieht:</p>
<pre><code>a b c d e
</code></pre>
<p>Ich habe es zwar auch schon geschafft, nur ist meine Lösung so unglaublich langsam, dass ich mir denke, dass das niemals die Beste sein kann.<br />
Bitte um Hilfe.<br />
Danke! <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1855277</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1855277</guid><dc:creator><![CDATA[Noob Otto]]></dc:creator><pubDate>Sun, 14 Feb 2010 04:53:26 GMT</pubDate></item><item><title><![CDATA[Reply to Doppelte (bzw. mehrfach) Vorkommende Leerzeichen in std::string löschen? on Sun, 14 Feb 2010 05:26:24 GMT]]></title><description><![CDATA[<p>Zeig mal deine Lösung</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1855280</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1855280</guid><dc:creator><![CDATA[dingdingdingding]]></dc:creator><pubDate>Sun, 14 Feb 2010 05:26:24 GMT</pubDate></item><item><title><![CDATA[Reply to Doppelte (bzw. mehrfach) Vorkommende Leerzeichen in std::string löschen? on Sun, 14 Feb 2010 09:32:41 GMT]]></title><description><![CDATA[<p>Eine sehr einfache Möglichkeit wäre es, einfach einen Stringstream und die Streamoperatoren zu benutzen. Das ist zwar sicherlich nicht die effizienteste Möglichkeit, aber es wird wird auch nicht spürbar langsam sein. Dafür braucht man sich aber wenig Gedanken um den eigentlichen Algorithmus machen, weil die Streamoperatoren alles für einen erledigen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1855299</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1855299</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Sun, 14 Feb 2010 09:32:41 GMT</pubDate></item><item><title><![CDATA[Reply to Doppelte (bzw. mehrfach) Vorkommende Leerzeichen in std::string löschen? on Sun, 14 Feb 2010 10:32:49 GMT]]></title><description><![CDATA[<p>Ich denke, viel schneller als so wirst du es nicht hinbekommen:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;algorithm&gt;
#include &lt;string&gt;
using namespace std;

bool isDoubleSpace(char first, char second)
{
	return first == second &amp;&amp; second == ' ';
}

int main()
{
	string foo = &quot;a   b c  de  f&quot;;
	string::iterator newEnd = unique(foo.begin(), foo.end(), isDoubleSpace);
	foo.resize(newEnd - foo.begin());
	cout &lt;&lt; foo;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1855331</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1855331</guid><dc:creator><![CDATA[Michael E.]]></dc:creator><pubDate>Sun, 14 Feb 2010 10:32:49 GMT</pubDate></item><item><title><![CDATA[Reply to Doppelte (bzw. mehrfach) Vorkommende Leerzeichen in std::string löschen? on Sun, 14 Feb 2010 12:14:37 GMT]]></title><description><![CDATA[<p><code>foo.resize(newEnd - foo.begin());</code><br />
-&gt;<br />
<code>foo.erase(newEnd, foo.begin());</code><br />
;o)</p>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1855378</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1855378</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Sun, 14 Feb 2010 12:14:37 GMT</pubDate></item><item><title><![CDATA[Reply to Doppelte (bzw. mehrfach) Vorkommende Leerzeichen in std::string löschen? on Sun, 14 Feb 2010 12:25:30 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">while( str.find( &quot;  &quot; ) != string::npos )
	std::replace( str.begin(), str.end(), &quot;  &quot;, &quot; &quot; );
</code></pre>
<p>Ungetestet.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1855379</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1855379</guid><dc:creator><![CDATA[EOP]]></dc:creator><pubDate>Sun, 14 Feb 2010 12:25:30 GMT</pubDate></item><item><title><![CDATA[Reply to Doppelte (bzw. mehrfach) Vorkommende Leerzeichen in std::string löschen? on Sun, 14 Feb 2010 12:30:26 GMT]]></title><description><![CDATA[<p>unskilled schrieb:</p>
<blockquote>
<p><code>foo.resize(newEnd - foo.begin());</code><br />
-&gt;<br />
<code>foo.erase(newEnd, foo.begin());</code><br />
;o)</p>
<p>bb</p>
</blockquote>
<p>Verdammt, das spart bei mir ne Abfrage <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>
<pre><code class="language-cpp">void __CLR_OR_THIS_CALL resize(size_type _Newsize, _Elem _Ch)
{	// determine new length, padding with _Ch elements as needed
	if (_Newsize &lt;= _Mysize)
		erase(_Newsize);
	else
		append(_Newsize - _Mysize, _Ch);
}
</code></pre>
<p>Das fällt wohl noch unter &quot;nicht viel schneller&quot; <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f921.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--clown_face"
      title=":clown:"
      alt="🤡"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1855383</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1855383</guid><dc:creator><![CDATA[Michael E.]]></dc:creator><pubDate>Sun, 14 Feb 2010 12:30:26 GMT</pubDate></item><item><title><![CDATA[Reply to Doppelte (bzw. mehrfach) Vorkommende Leerzeichen in std::string löschen? on Sun, 14 Feb 2010 12:33:07 GMT]]></title><description><![CDATA[<p>Da koennt man sich auch 'ne kleine RegEx basteln, bin aber grad zu faul mich durch die API zu wuehlen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1855384</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1855384</guid><dc:creator><![CDATA[Blue-Tiger]]></dc:creator><pubDate>Sun, 14 Feb 2010 12:33:07 GMT</pubDate></item><item><title><![CDATA[Reply to Doppelte (bzw. mehrfach) Vorkommende Leerzeichen in std::string löschen? on Sun, 14 Feb 2010 12:42:41 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;boost/regex.hpp&gt;
#include &lt;string&gt;

using namespace std;
using namespace boost;

int main()
{
	string s1 = &quot;a b  c d     e  f  &quot;;
	string s2 = regex_replace(s1, regex(&quot;\\s+&quot;), &quot; &quot;);
	cout &lt;&lt; s2 &lt;&lt; endl;
	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1855387</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1855387</guid><dc:creator><![CDATA[Blue-Tiger]]></dc:creator><pubDate>Sun, 14 Feb 2010 12:42:41 GMT</pubDate></item><item><title><![CDATA[Reply to Doppelte (bzw. mehrfach) Vorkommende Leerzeichen in std::string löschen? on Sun, 14 Feb 2010 14:18:34 GMT]]></title><description><![CDATA[<p>hmm.... die Regex-Variante ist langsamer als Michael E.'s Variante. (Die von EOP funktioniert nicht).</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;vector&gt;
#include &lt;boost/regex.hpp&gt;
#include &lt;boost/lexical_cast.hpp&gt;
#include &lt;boost/foreach.hpp&gt;
#define foreach BOOST_FOREACH

using namespace std;
using namespace boost;

bool isDoubleSpace(char first, char second)
{
	return first == second &amp;&amp; second == ' ';
}

double meth1(vector&lt;string&gt; str)
{
	double start = time(0);
	foreach(string&amp; s, str)
	{
		string::iterator newEnd = unique(s.begin(), s.end(), isDoubleSpace);
		s.erase(newEnd, s.begin());
		if (rand() == 42)
			cout &lt;&lt; s &lt;&lt; '\n';
	}
	double end = time(0);
	return end - start;
}

double meth2(vector&lt;string&gt; const&amp; str)
{
	double start = time(0);
	const regex r(&quot;\\s+&quot;, regex::optimize);
	const string space = &quot; &quot;;
	foreach(string&amp; s, str)
	{
		string s2 = regex_replace(s, r, space);
		if (rand() == 42)
			cout &lt;&lt; s2 &lt;&lt; '\n';
	}
	double end = time(0);
	return end - start;
}

int main(int argc, char** argv)
{
	if (argc &lt; 3)
	{
		cerr &lt;&lt; &quot;usage: test stringlength nstrings [space-prob]\n&quot;;
		return 1;
	}

	unsigned strlen = lexical_cast&lt;unsigned&gt;(argv[1]);
	unsigned nstrings = lexical_cast&lt;unsigned&gt;(argv[2]);
	double space_prob = (argc == 4) ? lexical_cast&lt;double&gt;(argv[3]) : 0.5;

	vector&lt;string&gt; str;
	str.reserve(nstrings);
	for (unsigned i = 0; i &lt; nstrings; ++i)
	{
		string tmp(strlen, 'a');
		for (unsigned j = 0; j &lt; strlen; ++j)
		{
			if (rand() / (double) RAND_MAX &lt; space_prob)
				tmp[j] = ' ';
		}
		str.push_back(tmp);
	}

	cout &lt;&lt; &quot;method 1: &quot; &lt;&lt; meth1(str) &lt;&lt; '\n';
	cout &lt;&lt; &quot;method 2: &quot; &lt;&lt; meth2(str) &lt;&lt; '\n';
	return 0;
}
</code></pre>
<pre><code>tom@blulap:~$ g++ test.cpp -o test -lboost_regex -O2 -DNDEBUG
tom@blulap:~$ ./test 100 1000000
method 1: 3
method 2: 38
tom@blulap:~$ ./test 100 1000000 0.1
method 1: 2
method 2: 24
tom@blulap:~$ ./test 100 1000000 0.9
method 1: 2
method 2: 15
tom@blulap:~$ ./test 200 1000000 0.9
method 1: 10
method 2: 27
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1855444</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1855444</guid><dc:creator><![CDATA[Blue-Tiger]]></dc:creator><pubDate>Sun, 14 Feb 2010 14:18:34 GMT</pubDate></item><item><title><![CDATA[Reply to Doppelte (bzw. mehrfach) Vorkommende Leerzeichen in std::string löschen? on Sun, 14 Feb 2010 14:50:56 GMT]]></title><description><![CDATA[<p>Wow, ich hätte wirklich nicht gedacht, dass ein Gast so viele Antworten bekommt! Danke schon einmal bis hierhin! Auch wenn es ein wenig unfair den anderen gegenüber ist, danke ich Blue-Tiger für das Benchmark ganz besonders!</p>
<p>Mein Lösungsweg sah so aus:</p>
<pre><code class="language-cpp">//...
while((uiPosTemp = stTest.find(' ', uiPosTemp + 1)) != std::string::npos)
  stTest.replace(uiPosTemp, stTest.find_first_not_of(' ', uiPosTemp) - uiPosTemp, &quot; &quot;);
//...
</code></pre>
<p>Wie geschrieben, der tut's auch, ist aber unglaublich langsam und bremst das gesamte restliche Programm aus.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1855471</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1855471</guid><dc:creator><![CDATA[Noob Otto]]></dc:creator><pubDate>Sun, 14 Feb 2010 14:50:56 GMT</pubDate></item><item><title><![CDATA[Reply to Doppelte (bzw. mehrfach) Vorkommende Leerzeichen in std::string löschen? on Sun, 14 Feb 2010 15:15:37 GMT]]></title><description><![CDATA[<p>Noch einmal wow, die Funktion von Michael E. ist weitaus schneller als meine eigene. Vielen, vielen Dank euch allen!!! <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /> <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /> <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1855492</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1855492</guid><dc:creator><![CDATA[Noob Otto]]></dc:creator><pubDate>Sun, 14 Feb 2010 15:15:37 GMT</pubDate></item><item><title><![CDATA[Reply to Doppelte (bzw. mehrfach) Vorkommende Leerzeichen in std::string löschen? on Sun, 14 Feb 2010 15:20:06 GMT]]></title><description><![CDATA[<p>... oder doch nicht? Wenn ich das, was Michael E. geschrieben hat, in eine Funktion packe und dann mit meiner Funktion vergleiche, ist meine schneller. Ich verstehe die Welt nicht mehr! <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1855494</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1855494</guid><dc:creator><![CDATA[Noob Otto]]></dc:creator><pubDate>Sun, 14 Feb 2010 15:20:06 GMT</pubDate></item><item><title><![CDATA[Reply to Doppelte (bzw. mehrfach) Vorkommende Leerzeichen in std::string löschen? on Sun, 14 Feb 2010 15:24:32 GMT]]></title><description><![CDATA[<p>Diesmal etwas, das funktioniert:</p>
<pre><code class="language-cpp">string str = &quot;a  b    c     d e  f   g  &quot;;
size_t pos = 0;

while( (pos = str.find( &quot;  &quot;, pos )) != string::npos )
	str.erase( pos, 1 );
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1855495</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1855495</guid><dc:creator><![CDATA[EOP]]></dc:creator><pubDate>Sun, 14 Feb 2010 15:24:32 GMT</pubDate></item><item><title><![CDATA[Reply to Doppelte (bzw. mehrfach) Vorkommende Leerzeichen in std::string löschen? on Sun, 14 Feb 2010 15:24:56 GMT]]></title><description><![CDATA[<p>Zeig doch mal, wie du deine Zeitmessungen machst.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1855496</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1855496</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Sun, 14 Feb 2010 15:24:56 GMT</pubDate></item><item><title><![CDATA[Reply to Doppelte (bzw. mehrfach) Vorkommende Leerzeichen in std::string löschen? on Sun, 14 Feb 2010 15:48:07 GMT]]></title><description><![CDATA[<p>Noob Otto schrieb:</p>
<blockquote>
<p>... oder doch nicht? Wenn ich das, was Michael E. geschrieben hat, in eine Funktion packe und dann mit meiner Funktion vergleiche, ist meine schneller. Ich verstehe die Welt nicht mehr!</p>
</blockquote>
<p>DAS ist am schnellsten</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;string&gt;
using namespace std;

void remove_spc (char *s) {
   char *t = s;
   char l = 0;
   while (*s) {
      if (*s != ' ' || l != ' ')
         *t++ = *s;
      l = *s++;
   }
   *t = 0;
}

int main (void)  {
   string a = &quot;a  b    c     d e  f   g  &quot;;
   char *p = (char*)a.c_str();
   remove_spc (p);
   a = p;
   cout &lt;&lt; a;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1855513</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1855513</guid><dc:creator><![CDATA[Next Generation Hacker]]></dc:creator><pubDate>Sun, 14 Feb 2010 15:48:07 GMT</pubDate></item><item><title><![CDATA[Reply to Doppelte (bzw. mehrfach) Vorkommende Leerzeichen in std::string löschen? on Sun, 14 Feb 2010 15:57:07 GMT]]></title><description><![CDATA[<p>Da wäre ich wirklich mal auf ein Benchmark gespannt <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="🙂"
    /> Denn mein Code macht im Prinzip nichts anderes, aber ich denke, die Standardalgorithmen werden sehr gut auf den jeweiligen Compiler hinoptimiert sein.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1855516</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1855516</guid><dc:creator><![CDATA[Michael E.]]></dc:creator><pubDate>Sun, 14 Feb 2010 15:57:07 GMT</pubDate></item><item><title><![CDATA[Reply to Doppelte (bzw. mehrfach) Vorkommende Leerzeichen in std::string löschen? on Sun, 14 Feb 2010 16:01:16 GMT]]></title><description><![CDATA[<p>Next Generation Hacker schrieb:</p>
<blockquote>
<p>DAS ist am schnellsten</p>
<pre><code class="language-cpp">Viel Mist
</code></pre>
</blockquote>
<ol>
<li>In nem C++-Programm ein C-cast -&gt; wenn dann const_cast!</li>
<li>Ist das echt böse! Du verändeeerst einen (nicht zu Unrecht) privaten Member, ohne auf andere Spezialitäten Rücksicht zu nehmen. z.B.</li>
<li>Du veränderst die Länge des strings!!! Woher weißt du, dass nicht intern eine Länge mitgespeichert wird?!?</li>
</ol>
<p>Du machst zwar später eine Zuweisung - aber muss ja nicht immer hinhauen...<br />
Lass die string-Klasse implizit geshared sein (k.A. wie der deutsche Begriff dafür ist...).<br />
Da wird der data-pointer einfach ins neue Objekt gesetzt, eine Kopie des Arrays findet erst satt, wenn das Objekt über die public-Funktionen verändert wird. Deine Methode verhindert ein korrektes Handling dieses Mechanismus!!!</p>
<pre><code class="language-cpp">doIt( const std::string&amp; str) {
    boeseFktDieStrAendert( (char*)str.c_str() );
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1855519</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1855519</guid><dc:creator><![CDATA[nervösling]]></dc:creator><pubDate>Sun, 14 Feb 2010 16:01:16 GMT</pubDate></item><item><title><![CDATA[Reply to Doppelte (bzw. mehrfach) Vorkommende Leerzeichen in std::string löschen? on Sun, 14 Feb 2010 16:13:57 GMT]]></title><description><![CDATA[<p>Release + Optimieruzngen kompilieren</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1855527</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1855527</guid><dc:creator><![CDATA[WelcomeToTheJungle]]></dc:creator><pubDate>Sun, 14 Feb 2010 16:13:57 GMT</pubDate></item><item><title><![CDATA[Reply to Doppelte (bzw. mehrfach) Vorkommende Leerzeichen in std::string löschen? on Sun, 14 Feb 2010 16:20:09 GMT]]></title><description><![CDATA[<p>Michael E. schrieb:</p>
<blockquote>
<p>Da wäre ich wirklich mal auf ein Benchmark gespannt</p>
</blockquote>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;algorithm&gt;
#include &lt;windows.h&gt;
using namespace std;

void remove_spc (char *s) {
   char *t = s;
   char l = 0;
   while (*s) {
      if (*s != ' ' || l != ' ')
         *t++ = *s;
      l = *s++;
   }
   *t = 0;
}

char *hacker()  {
   static char a[100];
   strcpy (a, &quot;a  b    c     d e  f   g  &quot;);
   remove_spc (a);
   return a;
}

bool isDoubleSpace(char first, char second)
{
    return first == second &amp;&amp; second == ' ';
}

string michael()
{
    string foo = &quot;a  b    c     d e  f   g  &quot;;
    string::iterator newEnd = unique(foo.begin(), foo.end(), isDoubleSpace);
    foo.resize(newEnd - foo.begin());
    return foo;
}

int main () {
   UINT32 t1, t2;
#define COUNT 1000000   

   t1 = GetTickCount();
   for (int s=0; s&lt;COUNT; s++)
      hacker();
   t1 = GetTickCount() - t1;

   t2 = GetTickCount();
   for (int s=0; s&lt;COUNT; s++)
      michael();
   t2 = GetTickCount() - t2;

   cout &lt;&lt; t1 &lt;&lt; endl;
   cout &lt;&lt; t2 &lt;&lt; endl;
}
</code></pre>
<p>Visual Studio, Release:<br />
Hacker 141 ms<br />
Michael 780 ms</p>
<p>Visual Studio, Debug:<br />
Hacker 858 ms<br />
Michael 26364 ms</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1855529</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1855529</guid><dc:creator><![CDATA[Next Generation Hacker]]></dc:creator><pubDate>Sun, 14 Feb 2010 16:20:09 GMT</pubDate></item><item><title><![CDATA[Reply to Doppelte (bzw. mehrfach) Vorkommende Leerzeichen in std::string löschen? on Sun, 14 Feb 2010 16:28:38 GMT]]></title><description><![CDATA[<p>Next Generation Hacker schrieb:</p>
<blockquote>
<p>Visual Studio, Debug:<br />
Hacker 858 ms<br />
Michael 26364 ms</p>
</blockquote>
<p>26Sekunden? Was hast du solange im Debug getrieben?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1855532</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1855532</guid><dc:creator><![CDATA[freaky]]></dc:creator><pubDate>Sun, 14 Feb 2010 16:28:38 GMT</pubDate></item><item><title><![CDATA[Reply to Doppelte (bzw. mehrfach) Vorkommende Leerzeichen in std::string löschen? on Sun, 14 Feb 2010 16:28:57 GMT]]></title><description><![CDATA[<p>Iteratorchecks auch im Release ausmachen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1855533</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1855533</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Sun, 14 Feb 2010 16:28:57 GMT</pubDate></item><item><title><![CDATA[Reply to Doppelte (bzw. mehrfach) Vorkommende Leerzeichen in std::string löschen? on Sun, 14 Feb 2010 16:31:45 GMT]]></title><description><![CDATA[<p>Na wir wollen doch fair bleiben und deiner Version auch mal das Erzeugen eines std::strings beibringen (so wie es gefordert war):</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;algorithm&gt;
#include &lt;windows.h&gt;
using namespace std;

void remove_spc (char *s) {
   char *t = s;
   char l = 0;
   while (*s) {
      if (*s != ' ' || l != ' ')
         *t++ = *s;
      l = *s++;
   }
   *t = 0;
}

string hacker()  {
	string a = &quot;a  b    c     d e  f   g  &quot;;
	char* p = (char*)a.c_str();
   remove_spc (p);
   a = p;
   return a;
}

bool isDoubleSpace(char first, char second)
{
    return first == second &amp;&amp; second == ' ';
}

string michael()
{
    string foo = &quot;a  b    c     d e  f   g  &quot;;
    string::iterator newEnd = unique(foo.begin(), foo.end(), isDoubleSpace);
    foo.resize(newEnd - foo.begin());
    return foo;
}

int main () {
   UINT32 t1, t2;
#define COUNT 1000000  

   t1 = GetTickCount();
   for (int s=0; s&lt;COUNT; s++)
      hacker();
   t1 = GetTickCount() - t1;

   t2 = GetTickCount();
   for (int s=0; s&lt;COUNT; s++)
      michael();
   t2 = GetTickCount() - t2;

   cout &lt;&lt; t1 &lt;&lt; endl;
   cout &lt;&lt; t2 &lt;&lt; endl;
}
</code></pre>
<p>Ergebnis (VS 2008 Release, volle Optimierung):<br />
687 (deins) gegen 579 (meins)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1855536</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1855536</guid><dc:creator><![CDATA[Michael E.]]></dc:creator><pubDate>Sun, 14 Feb 2010 16:31:45 GMT</pubDate></item><item><title><![CDATA[Reply to Doppelte (bzw. mehrfach) Vorkommende Leerzeichen in std::string löschen? on Sun, 14 Feb 2010 16:35:22 GMT]]></title><description><![CDATA[<p>Michael E. schrieb:</p>
<blockquote>
<p>Na wir wollen doch fair bleiben und deiner Version auch mal das Erzeugen eines std::strings beibringen (so wie es gefordert war):<br />
...<br />
Ergebnis (VS 2008 Release, volle Optimierung):<br />
687 (deins) gegen 579 (meins)</p>
</blockquote>
<p>Und die Moral von der Geschicht: Wenns schnell sein soll, nimm std::string nicht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1855538</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1855538</guid><dc:creator><![CDATA[Next Generation Hacker]]></dc:creator><pubDate>Sun, 14 Feb 2010 16:35:22 GMT</pubDate></item><item><title><![CDATA[Reply to Doppelte (bzw. mehrfach) Vorkommende Leerzeichen in std::string löschen? on Sun, 14 Feb 2010 16:39:26 GMT]]></title><description><![CDATA[<p>Next Generation Hacker schrieb:</p>
<blockquote>
<p>Und die Moral von der Geschicht: Wenns schnell sein soll, nimm std::string nicht.</p>
</blockquote>
<pre><code class="language-cpp">#define _SECURE_SCL 0

#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;algorithm&gt;
#include &lt;windows.h&gt;
using namespace std;

void remove_spc (char *s) {
   char *t = s;
   char l = 0;
   while (*s) {
      if (*s != ' ' || l != ' ')
         *t++ = *s;
      l = *s++;
   }
   *t = 0;
}

char *hacker()  {
   char* a = new char[100];
   strcpy (a, &quot;a  b    c     d e  f   g  &quot;);
   remove_spc (a);
   return a;
}

bool isDoubleSpace(char first, char second)
{
    return first == second &amp;&amp; second == ' ';
}

string michael()
{
    string foo = &quot;a  b    c     d e  f   g  &quot;;
    string::iterator newEnd = unique(foo.begin(), foo.end(), isDoubleSpace);
    foo.resize(newEnd - foo.begin());
    return foo;
}

int main () {
   UINT32 t1, t2;
#define COUNT 1000000  

   t1 = GetTickCount();
   for (int s=0; s&lt;COUNT; s++)
      hacker();
   t1 = GetTickCount() - t1;

   t2 = GetTickCount();
   for (int s=0; s&lt;COUNT; s++)
      michael();
   t2 = GetTickCount() - t2;

   cout &lt;&lt; t1 &lt;&lt; endl;
   cout &lt;&lt; t2 &lt;&lt; endl;
}
</code></pre>
<p>Hacker 266ms<br />
Michael 327ms</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1855543</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1855543</guid><dc:creator><![CDATA[life]]></dc:creator><pubDate>Sun, 14 Feb 2010 16:39:26 GMT</pubDate></item><item><title><![CDATA[Reply to Doppelte (bzw. mehrfach) Vorkommende Leerzeichen in std::string löschen? on Sun, 14 Feb 2010 16:39:32 GMT]]></title><description><![CDATA[<p>Ich warte aber definitiv lieber ne Sekunde bei der Ausführung als mich wochenlang bei der Entwicklung mit rohen char-Arrays rumzuplagen :p</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1855544</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1855544</guid><dc:creator><![CDATA[Michael E.]]></dc:creator><pubDate>Sun, 14 Feb 2010 16:39:32 GMT</pubDate></item></channel></rss>