<?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[Teile eines Strings zurückgeben]]></title><description><![CDATA[<p>Ich bin grad dabei eine Art Textarea Control zu schreiben. Dazu hab ich mir eine Miniklasse geschrieben die Strings zeilenweise zurückgibt. Jetzt wollte ich aber nicht immer eine Kopie eines Auschnittes aus einem String zurückgeben, da das Kopieren sicher recht lange dauert. Deshalb geb ich einen Iterator auf den Anfang und auf das Ende zurück</p>
<p>main.cpp</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &quot;../libs/stringlinewrapper.hpp&quot;

int main(void)
{
    StringLineWrapper str(10);
    std::string::iterator striter;
    STRINGITERATORS striters;
    str.GetString().append(&quot;Das ist ein Satz der laenger als 10 Zeichen ist.&quot;);
    for (uint i = 0; i &lt; str.GetLines(); ++i)
    {
        striters = str.GetLine(i);
        for (striter = striters.begin; striter &lt; striters.end; ++striter)
        {
            std::cout &lt;&lt; *striter;
        }
        std::cout &lt;&lt; std::endl;
    }
}
</code></pre>
<p>stringlinewrapper.cpp</p>
<pre><code class="language-cpp">#include &quot;stringlinewrapper.hpp&quot;

StringLineWrapper::StringLineWrapper(uint LineLength) : LineLength(LineLength) {}

STRINGITERATORS StringLineWrapper::GetLine(uint Line)
{
    if (Line &gt;= this-&gt;GetLines())
    {
        throw std::out_of_range(&quot;StringLineWrapper::GetLine() not in range&quot;);
    }

    STRINGITERATORS StrIterators;
    StrIterators.begin = this-&gt;String.begin() + Line * this-&gt;LineLength;
    if (Line == this-&gt;GetLines() - 1)
    {
        StrIterators.end = this-&gt;String.end();
    }
    else
    {
        StrIterators.end = StrIterators.begin + this-&gt;LineLength;
    }
    return StrIterators;
}

uint StringLineWrapper::GetLines(void)
{
    return this-&gt;String.length() / this-&gt;LineLength + 1;
}

std::string &amp;StringLineWrapper::GetString(void)
{
    return String;
}
</code></pre>
<p>stringlinewrapper.hpp</p>
<pre><code class="language-cpp">#include &lt;stdexcept&gt;
#include &quot;CustomTypenames.h&quot;

typedef struct _StringIterators {
    std::string::iterator begin;
    std::string::iterator end;
} STRINGITERATORS, *LPSTRINGITERATORS;

class StringLineWrapper
{
private:

    std::string String;
    uint LineLength;

public:

    StringLineWrapper(uint LineLength);
    STRINGITERATORS GetLine(uint Line);
    uint GetLines(void);
    std::string &amp;GetString(void);
};
</code></pre>
<p>(Den Code hab ich jetzt nur in ein paar Minuten hingeschrieben. Nur um das Prinzip zu zeigen.)</p>
<p>Nun wollte ich fragen, ob das eine sinnvolle Möglichkeit ist dieses Problem zu lösen und ob es vielleicht noch bessere bzw. schnellere Möglichkeiten gibt, da es in meinem Programm ziemlich auf die Performance ankommt.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/234076/teile-eines-strings-zurückgeben</link><generator>RSS for Node</generator><lastBuildDate>Thu, 24 Sep 2026 13:16:16 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/234076.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 12 Feb 2009 18:17:55 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Teile eines Strings zurückgeben on Thu, 12 Feb 2009 18:17:55 GMT]]></title><description><![CDATA[<p>Ich bin grad dabei eine Art Textarea Control zu schreiben. Dazu hab ich mir eine Miniklasse geschrieben die Strings zeilenweise zurückgibt. Jetzt wollte ich aber nicht immer eine Kopie eines Auschnittes aus einem String zurückgeben, da das Kopieren sicher recht lange dauert. Deshalb geb ich einen Iterator auf den Anfang und auf das Ende zurück</p>
<p>main.cpp</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &quot;../libs/stringlinewrapper.hpp&quot;

int main(void)
{
    StringLineWrapper str(10);
    std::string::iterator striter;
    STRINGITERATORS striters;
    str.GetString().append(&quot;Das ist ein Satz der laenger als 10 Zeichen ist.&quot;);
    for (uint i = 0; i &lt; str.GetLines(); ++i)
    {
        striters = str.GetLine(i);
        for (striter = striters.begin; striter &lt; striters.end; ++striter)
        {
            std::cout &lt;&lt; *striter;
        }
        std::cout &lt;&lt; std::endl;
    }
}
</code></pre>
<p>stringlinewrapper.cpp</p>
<pre><code class="language-cpp">#include &quot;stringlinewrapper.hpp&quot;

StringLineWrapper::StringLineWrapper(uint LineLength) : LineLength(LineLength) {}

STRINGITERATORS StringLineWrapper::GetLine(uint Line)
{
    if (Line &gt;= this-&gt;GetLines())
    {
        throw std::out_of_range(&quot;StringLineWrapper::GetLine() not in range&quot;);
    }

    STRINGITERATORS StrIterators;
    StrIterators.begin = this-&gt;String.begin() + Line * this-&gt;LineLength;
    if (Line == this-&gt;GetLines() - 1)
    {
        StrIterators.end = this-&gt;String.end();
    }
    else
    {
        StrIterators.end = StrIterators.begin + this-&gt;LineLength;
    }
    return StrIterators;
}

uint StringLineWrapper::GetLines(void)
{
    return this-&gt;String.length() / this-&gt;LineLength + 1;
}

std::string &amp;StringLineWrapper::GetString(void)
{
    return String;
}
</code></pre>
<p>stringlinewrapper.hpp</p>
<pre><code class="language-cpp">#include &lt;stdexcept&gt;
#include &quot;CustomTypenames.h&quot;

typedef struct _StringIterators {
    std::string::iterator begin;
    std::string::iterator end;
} STRINGITERATORS, *LPSTRINGITERATORS;

class StringLineWrapper
{
private:

    std::string String;
    uint LineLength;

public:

    StringLineWrapper(uint LineLength);
    STRINGITERATORS GetLine(uint Line);
    uint GetLines(void);
    std::string &amp;GetString(void);
};
</code></pre>
<p>(Den Code hab ich jetzt nur in ein paar Minuten hingeschrieben. Nur um das Prinzip zu zeigen.)</p>
<p>Nun wollte ich fragen, ob das eine sinnvolle Möglichkeit ist dieses Problem zu lösen und ob es vielleicht noch bessere bzw. schnellere Möglichkeiten gibt, da es in meinem Programm ziemlich auf die Performance ankommt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1662724</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1662724</guid><dc:creator><![CDATA[Caster]]></dc:creator><pubDate>Thu, 12 Feb 2009 18:17:55 GMT</pubDate></item><item><title><![CDATA[Reply to Teile eines Strings zurückgeben on Thu, 12 Feb 2009 18:39:14 GMT]]></title><description><![CDATA[<p>Also wenn du wirklich auf die Performance gehen willst, dann würde ich das eher mit den C-Strings machen, als std::string. Da kommen halt so Sachen, wie Boundchecks noch rein, aber die kannst du ja optional machen ( Makros ) so, dass der Benutzer selbst entscheiden kann, ob er sich selbst um die Gültigkeit kümmern will oder nicht.</p>
<p>Ansonsten gibt es hier:<br />
<a href="http://www.cplusplus.com/reference/string/string/substr.html" rel="nofollow">http://www.cplusplus.com/reference/string/string/substr.html</a></p>
<p>string Funktionalität, die tendenziell wahrscheinlich schneller ist, als etwas selbstgebasteltes.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1662735</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1662735</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Thu, 12 Feb 2009 18:39:14 GMT</pubDate></item><item><title><![CDATA[Reply to Teile eines Strings zurückgeben on Thu, 12 Feb 2009 19:11:22 GMT]]></title><description><![CDATA[<p><a href="http://www.boost.org/doc/libs/1_37_0/libs/range/doc/utility_class.html" rel="nofollow">boost::range</a> ist hier wohl genau das was du suchst.</p>
<p>Im Prinzip eignet sich natuerlich hier auch eine eigene string klasse die nicht kopiert sondern nur zeiger in den string hinein besitzt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1662765</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1662765</guid><dc:creator><![CDATA[Shade Of Mine]]></dc:creator><pubDate>Thu, 12 Feb 2009 19:11:22 GMT</pubDate></item><item><title><![CDATA[Reply to Teile eines Strings zurückgeben on Fri, 13 Feb 2009 22:58:04 GMT]]></title><description><![CDATA[<p>Danke für die Antworten. Ich werde mir dann wohl meine eigene kleine Stringklasse schreiben.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1663535</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1663535</guid><dc:creator><![CDATA[Caster]]></dc:creator><pubDate>Fri, 13 Feb 2009 22:58:04 GMT</pubDate></item><item><title><![CDATA[Reply to Teile eines Strings zurückgeben on Fri, 13 Feb 2009 22:59:58 GMT]]></title><description><![CDATA[<p>Caster schrieb:</p>
<blockquote>
<p>Danke für die Antworten. Ich werde mir dann wohl meine eigene kleine Stringklasse schreiben.</p>
</blockquote>
<p><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/1663536</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1663536</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Fri, 13 Feb 2009 22:59:58 GMT</pubDate></item></channel></rss>