<?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[Prüfen, ob ein std::string nur Whitespaces enthält.]]></title><description><![CDATA[<p>Guten Abend,</p>
<p>wie im Titel erwähnt, will ich prüfen, ob ein <code>string</code> nur aus Whitespaces besteht. Momentan mache ich es so:</p>
<pre><code class="language-cpp">bool only_whitespace(const string&amp; str)
{
	string::const_iterator it = find_if(begin(str),end(str),[](char c)
	{
		return c!=' ';
	});
	return it==end(str);
}
</code></pre>
<p>Meine Frage ist nun, ob es noch eine elegantere C++-Lösung gibt?</p>
<p>Danke im Voraus.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/301022/prüfen-ob-ein-std-string-nur-whitespaces-enthält</link><generator>RSS for Node</generator><lastBuildDate>Wed, 12 Aug 2026 11:19:56 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/301022.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 16 Mar 2012 20:32:11 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Prüfen, ob ein std::string nur Whitespaces enthält. on Fri, 16 Mar 2012 20:32:11 GMT]]></title><description><![CDATA[<p>Guten Abend,</p>
<p>wie im Titel erwähnt, will ich prüfen, ob ein <code>string</code> nur aus Whitespaces besteht. Momentan mache ich es so:</p>
<pre><code class="language-cpp">bool only_whitespace(const string&amp; str)
{
	string::const_iterator it = find_if(begin(str),end(str),[](char c)
	{
		return c!=' ';
	});
	return it==end(str);
}
</code></pre>
<p>Meine Frage ist nun, ob es noch eine elegantere C++-Lösung gibt?</p>
<p>Danke im Voraus.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2192240</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2192240</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Fri, 16 Mar 2012 20:32:11 GMT</pubDate></item><item><title><![CDATA[Reply to Prüfen, ob ein std::string nur Whitespaces enthält. on Fri, 16 Mar 2012 20:41:43 GMT]]></title><description><![CDATA[<p>all_of/any_of</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2192243</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2192243</guid><dc:creator><![CDATA[314159265358979]]></dc:creator><pubDate>Fri, 16 Mar 2012 20:41:43 GMT</pubDate></item><item><title><![CDATA[Reply to Prüfen, ob ein std::string nur Whitespaces enthält. on Fri, 16 Mar 2012 21:11:59 GMT]]></title><description><![CDATA[<p>314159265358979 schrieb:</p>
<blockquote>
<p>all_of/any_of</p>
</blockquote>
<p>Danke dir. Und hier noch die Lösung zu PIs Vorschlag für andere Personen.</p>
<pre><code class="language-cpp">bool only_whitespace(const string&amp; str)
{
	return all_of(begin(str),end(str),[](char c)
	{
		return c==' ';
	});
}
</code></pre>
<p>Was ich an <code>all_of</code> aber blöd finde ist, dass für eine empty-range <code>true</code> zurückgegeben wird.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2192248</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2192248</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Fri, 16 Mar 2012 21:11:59 GMT</pubDate></item><item><title><![CDATA[Reply to Prüfen, ob ein std::string nur Whitespaces enthält. on Fri, 16 Mar 2012 21:10:05 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">return !str.empty() &amp;&amp; all_of(...);
</code></pre>
<p>Allerdings: Wenn es dir nur um ' ' und nicht auch um andere Whitespaces geht, dürfte</p>
<pre><code class="language-cpp">return !str.empty() &amp;&amp; str.find_first_not_of(' ') != std::string::npos;
</code></pre>
<p>performanter sein.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2192250</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2192250</guid><dc:creator><![CDATA[seldon]]></dc:creator><pubDate>Fri, 16 Mar 2012 21:10:05 GMT</pubDate></item><item><title><![CDATA[Reply to Prüfen, ob ein std::string nur Whitespaces enthält. on Fri, 16 Mar 2012 21:44:29 GMT]]></title><description><![CDATA[<p>all_of ist nur sinnvoll in Kombination mit isspace (was wohl auch gefragt sein dürfte), sonst ist das first_not_of geeigneter.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2192260</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2192260</guid><dc:creator><![CDATA[allofso]]></dc:creator><pubDate>Fri, 16 Mar 2012 21:44:29 GMT</pubDate></item><item><title><![CDATA[Reply to Prüfen, ob ein std::string nur Whitespaces enthält. on Fri, 16 Mar 2012 21:46:02 GMT]]></title><description><![CDATA[<p>seldon schrieb:</p>
<blockquote>
<pre><code class="language-cpp">return !str.empty() &amp;&amp; all_of(...);
</code></pre>
<p>Allerdings: Wenn es dir nur um ' ' und nicht auch um andere Whitespaces geht, dürfte</p>
<pre><code class="language-cpp">return !str.empty() &amp;&amp; str.find_first_not_of(' ') != std::string::npos;
</code></pre>
<p>performanter sein.</p>
</blockquote>
<p>Wenn wir wirklich nur ein Zeichen haben und Höchstperformance gefragt ist, dann können wir sogar clevere reinterpret_casts auf den data()-Bereich des Strings machen und uns dann in ganzen Maschinenwörtern durch den String hangeln.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2192262</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2192262</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Fri, 16 Mar 2012 21:46:02 GMT</pubDate></item><item><title><![CDATA[Reply to Prüfen, ob ein std::string nur Whitespaces enthält. on Fri, 16 Mar 2012 22:35:31 GMT]]></title><description><![CDATA[<p>Das sollte eine Standardbibliothek insofern schön machen können, als dass in der Praxis (und das kann die Bibliothek dann intern wissen) &amp;str[0] immer für alle Datentypen richtig ausgerichtet sein dürfte (als Rückgabe von new[] halt). Wobei...wird das von benutzerdefinierten Allokatoren eigentlich auch verlangt?</p>
<p>Wie dem auch sei, 4 bzw. 8 Byte auf einmal sollte man da durchnudeln können, und ein Paradekandidat für SSE ist es eigentlich auch. Ich bin etwas verwundert, dass GNUs libstdc++ nichts derartiges macht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2192268</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2192268</guid><dc:creator><![CDATA[seldon]]></dc:creator><pubDate>Fri, 16 Mar 2012 22:35:31 GMT</pubDate></item><item><title><![CDATA[Reply to Prüfen, ob ein std::string nur Whitespaces enthält. on Fri, 16 Mar 2012 22:48:59 GMT]]></title><description><![CDATA[<p>seldon schrieb:</p>
<blockquote>
<p>Wie dem auch sei, 4 bzw. 8 Byte auf einmal sollte man da durchnudeln können, und ein Paradekandidat für SSE ist es eigentlich auch. Ich bin etwas verwundert, dass GNUs libstdc++ nichts derartiges macht.</p>
</blockquote>
<p>Naja, ist schon eine ziemlich spezielle Optimierung, die man bei diesem Problem benutzen kann und sonst eher schwerlich. Außerdem ist das ein guter Einwand mit benutzerdefiniertem Allokator, da bin ich mir nicht sicher (und ich bin auch zu faul nachzugucken), ob die spezielle Anforderungen erfüllen müssen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2192270</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2192270</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Fri, 16 Mar 2012 22:48:59 GMT</pubDate></item><item><title><![CDATA[Reply to Prüfen, ob ein std::string nur Whitespaces enthält. on Fri, 16 Mar 2012 23:37:25 GMT]]></title><description><![CDATA[<p>Ggf. behandelt man dann die ersten drei bis sieben Byte halt gesondert. Spannend wird es allerdings, wenn std::basic_string mit etwas anderem als std::char_traits&lt;char&gt; bzw. std::char_traits&lt;wchar_t&gt; konkretisiert wird.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2192280</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2192280</guid><dc:creator><![CDATA[seldon]]></dc:creator><pubDate>Fri, 16 Mar 2012 23:37:25 GMT</pubDate></item><item><title><![CDATA[Reply to Prüfen, ob ein std::string nur Whitespaces enthält. on Sat, 17 Mar 2012 00:08:00 GMT]]></title><description><![CDATA[<p>seldon schrieb:</p>
<blockquote>
<p>Ggf. behandelt man dann die ersten drei bis sieben Byte halt gesondert. Spannend wird es allerdings, wenn std::basic_string mit etwas anderem als std::char_traits&lt;char&gt; bzw. std::char_traits&lt;wchar_t&gt; konkretisiert wird.</p>
</blockquote>
<p>Naja das liesse sich ja leicht über Spezialisierungen regeln.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2192285</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2192285</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Sat, 17 Mar 2012 00:08:00 GMT</pubDate></item></channel></rss>