<?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[Eine Frage zum Vector]]></title><description><![CDATA[<p>Hallo, ich lese hier Daten in einen Vector ein welche ich Empfangen habe.<br />
Mein Vector hat hier ja eine Statische größe von 30 Byte und ich lese hier immer Datenpakete mit einer größe von Maximal 10 Byte ein.</p>
<p>Beispiel 1:</p>
<pre><code class="language-cpp">int rc; // recv gibt die Anzahl der empfangenen Bytes zurück

vector&lt;char&gt;ans(30);
int TotalReceivedBytes=0;
do
{
rc = recv(client,&amp;ans [TotalReceivedBytes],10,0);

        if ( rc &gt; 0 )
        {
            cout&lt;&lt;&quot;Gelesene Bytes: &quot; &lt;&lt; rc &lt;&lt; endl;   // 10

            TotalReceivedBytes = rc + TotalReceivedBytes;
            cout&lt;&lt;&quot;Ausgabe: &quot; &lt;&lt; ans[ TotalReceivedBytes -1 ]&lt;&lt;endl;

        }

        if (rc==0)
        {
           cout&lt;&lt;&quot;Server hat die Verbindung getrennt..&quot;&lt;&lt;endl;
           system(&quot;PAUSE&quot;);
           return 0;
        }
        if (rc==SOCKET_ERROR)
        {
                cout&lt;&lt;&quot;SOCKET_ERROR&quot;&lt;&lt;endl;
                system(&quot;PAUSE&quot;);
                return 0;
        }

}
while( ans[ TotalReceivedBytes-1 ] != 'X' );
cout&lt;&lt;&quot;--&gt; &quot; &lt;&lt; &amp;ans[0] &lt;&lt; endl;
</code></pre>
<p>Hier lese ich immer nur Datenpakete bis maximal 1 Byte ein.<br />
Das Empfangene Paket pushe ich in einen Vector.</p>
<p>Ich denke mal ich kann so oft push_back machen bis mein Arbeitsspeicher voll ist bzw. bei einem 32Bit Prozess bis max 4 GB ( Win 32). (Ist das richtig?)</p>
<p>Beispiel 2:</p>
<pre><code class="language-cpp">int rc; // recv gibt die Anzahl der empfangenen Bytes zurück

vector&lt;char&gt;ans(1);
vector&lt;char&gt;Buffer;

int TotalReceivedBytes=0;

int j=0;
do
{
rc = recv(client,&amp;ans [0], 1 ,0);

        if ( rc &gt; 0 )
        {
            TotalReceivedBytes = rc + TotalReceivedBytes;
            Buffer.push_back( ans[0] );
            j++;
        }

        if (rc==0)
        {
           cout&lt;&lt;&quot;Server hat die Verbindung getrennt..&quot;&lt;&lt;endl;
           system(&quot;PAUSE&quot;);
           return 0;
        }
        if (rc==SOCKET_ERROR)
        {
                cout&lt;&lt;&quot;SOCKET_ERROR&quot;&lt;&lt;endl;
                system(&quot;PAUSE&quot;);
                return 0;
        }

}
while( Buffer[j-1] != 'X' );

Buffer[TotalReceivedBytes] = '\x00';
cout&lt;&lt;&quot;--&gt; &quot; &lt;&lt; &amp;Buffer[0] &lt;&lt; endl;
</code></pre>
<p>Worauf ich hinauswill ich lese in Beispiel 2 ja immer nur 1 Byte ich würde jedoch gerne 10 Byte jedes mal lesen und trotzdem nicht auf die push_back Funktion verzichten sodass ich also jedes mal 10 Byte lese und diese dann in den Vector pushe.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/306856/eine-frage-zum-vector</link><generator>RSS for Node</generator><lastBuildDate>Fri, 07 Aug 2026 07:48:57 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/306856.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 09 Aug 2012 09:25:21 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Eine Frage zum Vector on Thu, 09 Aug 2012 09:25:21 GMT]]></title><description><![CDATA[<p>Hallo, ich lese hier Daten in einen Vector ein welche ich Empfangen habe.<br />
Mein Vector hat hier ja eine Statische größe von 30 Byte und ich lese hier immer Datenpakete mit einer größe von Maximal 10 Byte ein.</p>
<p>Beispiel 1:</p>
<pre><code class="language-cpp">int rc; // recv gibt die Anzahl der empfangenen Bytes zurück

vector&lt;char&gt;ans(30);
int TotalReceivedBytes=0;
do
{
rc = recv(client,&amp;ans [TotalReceivedBytes],10,0);

        if ( rc &gt; 0 )
        {
            cout&lt;&lt;&quot;Gelesene Bytes: &quot; &lt;&lt; rc &lt;&lt; endl;   // 10

            TotalReceivedBytes = rc + TotalReceivedBytes;
            cout&lt;&lt;&quot;Ausgabe: &quot; &lt;&lt; ans[ TotalReceivedBytes -1 ]&lt;&lt;endl;

        }

        if (rc==0)
        {
           cout&lt;&lt;&quot;Server hat die Verbindung getrennt..&quot;&lt;&lt;endl;
           system(&quot;PAUSE&quot;);
           return 0;
        }
        if (rc==SOCKET_ERROR)
        {
                cout&lt;&lt;&quot;SOCKET_ERROR&quot;&lt;&lt;endl;
                system(&quot;PAUSE&quot;);
                return 0;
        }

}
while( ans[ TotalReceivedBytes-1 ] != 'X' );
cout&lt;&lt;&quot;--&gt; &quot; &lt;&lt; &amp;ans[0] &lt;&lt; endl;
</code></pre>
<p>Hier lese ich immer nur Datenpakete bis maximal 1 Byte ein.<br />
Das Empfangene Paket pushe ich in einen Vector.</p>
<p>Ich denke mal ich kann so oft push_back machen bis mein Arbeitsspeicher voll ist bzw. bei einem 32Bit Prozess bis max 4 GB ( Win 32). (Ist das richtig?)</p>
<p>Beispiel 2:</p>
<pre><code class="language-cpp">int rc; // recv gibt die Anzahl der empfangenen Bytes zurück

vector&lt;char&gt;ans(1);
vector&lt;char&gt;Buffer;

int TotalReceivedBytes=0;

int j=0;
do
{
rc = recv(client,&amp;ans [0], 1 ,0);

        if ( rc &gt; 0 )
        {
            TotalReceivedBytes = rc + TotalReceivedBytes;
            Buffer.push_back( ans[0] );
            j++;
        }

        if (rc==0)
        {
           cout&lt;&lt;&quot;Server hat die Verbindung getrennt..&quot;&lt;&lt;endl;
           system(&quot;PAUSE&quot;);
           return 0;
        }
        if (rc==SOCKET_ERROR)
        {
                cout&lt;&lt;&quot;SOCKET_ERROR&quot;&lt;&lt;endl;
                system(&quot;PAUSE&quot;);
                return 0;
        }

}
while( Buffer[j-1] != 'X' );

Buffer[TotalReceivedBytes] = '\x00';
cout&lt;&lt;&quot;--&gt; &quot; &lt;&lt; &amp;Buffer[0] &lt;&lt; endl;
</code></pre>
<p>Worauf ich hinauswill ich lese in Beispiel 2 ja immer nur 1 Byte ich würde jedoch gerne 10 Byte jedes mal lesen und trotzdem nicht auf die push_back Funktion verzichten sodass ich also jedes mal 10 Byte lese und diese dann in den Vector pushe.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2240317</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2240317</guid><dc:creator><![CDATA[_mat_]]></dc:creator><pubDate>Thu, 09 Aug 2012 09:25:21 GMT</pubDate></item><item><title><![CDATA[Reply to Eine Frage zum Vector on Thu, 09 Aug 2012 09:30:40 GMT]]></title><description><![CDATA[<p>Wenn es mit dem push_back nicht geht dann würde ich es wohl mit string und append machen:</p>
<pre><code class="language-cpp">vector&lt;char&gt;ans(10);
string dbg;
do
{
rc = recv(client,&amp;ans[0],10,0);

        if ( rc &gt; 0 )
        {
            cout&lt;&lt;&quot;Gelesene Bytes: &quot; &lt;&lt; rc &lt;&lt; endl;
            ans[rc]='\x00';
            dbg.append(  &amp;ans[0] );

        }

        if (rc==0)
        {
           cout&lt;&lt;&quot;Server hat die Verbindung getrennt..&quot;&lt;&lt;endl;
           system(&quot;PAUSE&quot;);
           return 0;
        }
        if (rc==SOCKET_ERROR)
        {
                cout&lt;&lt;&quot;SOCKET_ERROR&quot;&lt;&lt;endl;
                system(&quot;PAUSE&quot;);
                return 0;
        }

}
while( ans[ rc - 1 ] != 'X' );

cout&lt;&lt;&quot;--&gt; &quot; &lt;&lt; dbg &lt;&lt; endl;
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2240319</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2240319</guid><dc:creator><![CDATA[_mat_]]></dc:creator><pubDate>Thu, 09 Aug 2012 09:30:40 GMT</pubDate></item><item><title><![CDATA[Reply to Eine Frage zum Vector on Thu, 09 Aug 2012 09:31:55 GMT]]></title><description><![CDATA[<p>Vergrößere den Empfangspuffer um die erwarteten 10 Elemente, dann gibst du der Lesefunktion die Adresse des ersten neu erstellten Elements.</p>
<pre><code class="language-cpp">vector&lt;char&gt;ans(1);
</code></pre>
<p>Nimm doch einfach einen char!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2240320</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2240320</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Thu, 09 Aug 2012 09:31:55 GMT</pubDate></item><item><title><![CDATA[Reply to Eine Frage zum Vector on Thu, 09 Aug 2012 09:58:07 GMT]]></title><description><![CDATA[<blockquote>
<p>C/C++ Code:<br />
vector&lt;char&gt;ans(1);<br />
Nimm doch einfach einen char!</p>
</blockquote>
<p><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>
<p>Das passt doch gar nicht, ich glaub ich hab dich irgendwie missverstanden ...</p>
<pre><code class="language-cpp">int rc; // recv gibt die Anzahl der empfangenen Bytes zurück
vector&lt;char&gt;ans(1);

do
{
rc = recv(client,&amp;ans [0], 1 ,0);

        if ( rc &gt; 0 )
        {

               ....
        }

}
while( Buffer[j-1] != 'X' );

Buffer[TotalReceivedBytes] = '\x00';
cout&lt;&lt;&quot;--&gt; &quot; &lt;&lt; &amp;Buffer[0] &lt;&lt; endl;
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2240323</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2240323</guid><dc:creator><![CDATA[_mat_]]></dc:creator><pubDate>Thu, 09 Aug 2012 09:58:07 GMT</pubDate></item><item><title><![CDATA[Reply to Eine Frage zum Vector on Thu, 09 Aug 2012 10:12:30 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">char ans;
// ...
rc = recv(client, &amp;ans, 1 ,0);
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2240328</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2240328</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Thu, 09 Aug 2012 10:12:30 GMT</pubDate></item><item><title><![CDATA[Reply to Eine Frage zum Vector on Thu, 09 Aug 2012 11:06:37 GMT]]></title><description><![CDATA[<p>Aber so lese ich ja wieder nur Byte für Byte, ich wollte ja in 10 Byte blöcken lesen.</p>
<p>Ich müsste an den Vector dbg die Daten aus dem Vector ans anhängen.<br />
Gibt es sowas wie append für Vector?</p>
<pre><code class="language-cpp">vector&lt;char&gt;ans(10);
vector&lt;char&gt;dbg;

int TotalReceivedBytes=0;
do
{
rc = recv(client,&amp;ans[0],10,0);

        if ( rc &gt; 0 )
        {
            cout&lt;&lt;&quot;Gelesene Bytes: &quot; &lt;&lt; rc &lt;&lt; endl;
            TotalReceivedBytes = rc + TotalReceivedBytes;

            dbg.resize( TotalReceivedBytes );
            dbg.append( ans ); //Gibts so nicht ..

        }

}
while( ans[ rc - 1 ] != 'X' );

cout&lt;&lt;&quot;--&gt; &quot; &lt;&lt; &amp;dbg[0] &lt;&lt; endl;
</code></pre>
<p>Btw. was ist eigentlich am Perfomantesten jedes Byte wirklich einzelt lesen und dann in einen Vector pushen oder immer direkt die Daten in Paketen von z.b. 100 Byte oder so lesen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2240345</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2240345</guid><dc:creator><![CDATA[_mat_]]></dc:creator><pubDate>Thu, 09 Aug 2012 11:06:37 GMT</pubDate></item><item><title><![CDATA[Reply to Eine Frage zum Vector on Thu, 09 Aug 2012 11:18:01 GMT]]></title><description><![CDATA[<p>Wie du mehr als 1 Byte lesen kannst, habe ich dir doch schon gesagt. Lies Antworten bitte genau! Das <code>vector&lt;char&gt; ans(1)</code> vs. <code>char ans</code> war doch nur ein Nebenschauplatz, der dir den Unsinn eines statischen Vectors mit Länge 1 zeigen sollte. Überhaupt sind kleine statische vectoren recht sinnlos. Es sieht so aus, als hättest du einmal vorgemacht bekommen hast, wie man in C++ in einen vector einliest und nun wandelst du das Kochrezept ab, ohne verstanden zu haben, wieso überhaupt ein vector benutzt wurde!</p>
<blockquote>
<p>Btw. was ist eigentlich am Perfomantesten jedes Byte wirklich einzelt lesen und dann in einen Vector pushen oder immer direkt die Daten in Paketen von z.b. 100 Byte oder so lesen?</p>
</blockquote>
<p>Kommt drauf an, woher die Zeichen kommen. In der Regel sind Blockoperationen wesentlich schneller.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2240351</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2240351</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Thu, 09 Aug 2012 11:18:01 GMT</pubDate></item><item><title><![CDATA[Reply to Eine Frage zum Vector on Thu, 09 Aug 2012 11:43:54 GMT]]></title><description><![CDATA[<p>Ich glaub ich hab es geschafft:</p>
<pre><code class="language-cpp">int TotalReceivedBytes=0;
vector&lt;char&gt;ans;
int inc = 10; // Anzahl um die der Vector erhöht werden soll nachjedem Schleifen durchlauf

do
{
ans.resize ( TotalReceivedBytes + inc );

rc = recv(client,&amp;ans [TotalReceivedBytes], inc ,0);

        if ( rc &gt; 0 )
        {
            TotalReceivedBytes = rc + TotalReceivedBytes;
        }

}
while( ans[ TotalReceivedBytes-1 ] != 'X' );
</code></pre>
<p>Eine Frage hab ich aber noch auhc wenn die hier nicht ganz hingehört.<br />
Welche Block größe ist den sinnvoll, ich mein soll ich 10 Byte aufeinmal einlesen oder doch lieber 100 Byte?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2240361</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2240361</guid><dc:creator><![CDATA[_mat_]]></dc:creator><pubDate>Thu, 09 Aug 2012 11:43:54 GMT</pubDate></item><item><title><![CDATA[Reply to Eine Frage zum Vector on Thu, 09 Aug 2012 12:49:07 GMT]]></title><description><![CDATA[<p><em>mat</em> schrieb:</p>
<blockquote>
<p>Welche Block größe ist den sinnvoll, ich mein soll ich 10 Byte aufeinmal einlesen oder doch lieber 100 Byte?</p>
</blockquote>
<p>Such dir eine der folgenden Antworten aus:<br />
1. Kommt drauf an.<br />
2. Probier es aus.<br />
<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>Ohne Hintergrundinformation kann man keine Optimierungsfragen beantworten. Es ist noch nicht einmal klar, was optimiert werden soll.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2240389</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2240389</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Thu, 09 Aug 2012 12:49:07 GMT</pubDate></item><item><title><![CDATA[Reply to Eine Frage zum Vector on Thu, 09 Aug 2012 13:24:01 GMT]]></title><description><![CDATA[<p>In meinem Fall geht es um das versenden von Emails und das hochladen von Dateien.<br />
Alles über das HTTP-Protokoll.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2240404</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2240404</guid><dc:creator><![CDATA[_mat_]]></dc:creator><pubDate>Thu, 09 Aug 2012 13:24:01 GMT</pubDate></item></channel></rss>