<?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[vector effizient füllen]]></title><description><![CDATA[<p>Hi,</p>
<p>nehmen wir mal an ich habe einen vector, den ich füllen will. Glücklicherweise weiß ich schon, daß ich genau 1000 Elemente reinpacken will :p Wie geht das effinzient?</p>
<p>Möglichkeit 1:</p>
<pre><code class="language-cpp">...
vector&lt;double&gt; v;
for (int i=0; i&lt;1000; ++i)
     {x = ZufaelligeDoubleZahl();
      v.push_back(x);
     }
...
</code></pre>
<p>Möglichkeit 2:</p>
<pre><code class="language-cpp">...
vector&lt;double&gt; v (1000);
for (int i=0; i&lt;v.size(); ++i)
     v[i] = ZufaelligeDoubleZahl();
...
</code></pre>
<p>Ich denke ja, Möglichkeit 2 wäre effizienter, weil keine reskalierung des Vektors zu erfolgen hat. Oder ist jemand anderer Meinung?</p>
<p>Gruß gAsT</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/122480/vector-effizient-füllen</link><generator>RSS for Node</generator><lastBuildDate>Sun, 23 Aug 2026 07:14:02 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/122480.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 06 Oct 2005 09:29:02 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to vector effizient füllen on Thu, 06 Oct 2005 09:29:02 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>nehmen wir mal an ich habe einen vector, den ich füllen will. Glücklicherweise weiß ich schon, daß ich genau 1000 Elemente reinpacken will :p Wie geht das effinzient?</p>
<p>Möglichkeit 1:</p>
<pre><code class="language-cpp">...
vector&lt;double&gt; v;
for (int i=0; i&lt;1000; ++i)
     {x = ZufaelligeDoubleZahl();
      v.push_back(x);
     }
...
</code></pre>
<p>Möglichkeit 2:</p>
<pre><code class="language-cpp">...
vector&lt;double&gt; v (1000);
for (int i=0; i&lt;v.size(); ++i)
     v[i] = ZufaelligeDoubleZahl();
...
</code></pre>
<p>Ich denke ja, Möglichkeit 2 wäre effizienter, weil keine reskalierung des Vektors zu erfolgen hat. Oder ist jemand anderer Meinung?</p>
<p>Gruß gAsT</p>
]]></description><link>https://www.c-plusplus.net/forum/post/886255</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/886255</guid><dc:creator><![CDATA[gAsT_11:30]]></dc:creator><pubDate>Thu, 06 Oct 2005 09:29:02 GMT</pubDate></item><item><title><![CDATA[Reply to vector effizient füllen on Thu, 06 Oct 2005 09:32:28 GMT]]></title><description><![CDATA[<p>M1 hat das problem, daß beim wachsen ein paar mal alles umkopiert wird.</p>
<p>M2 hat das problem, daß vorher alles genullt wird.</p>
<p>Möglichkeit 3:</p>
<pre><code class="language-cpp">...
vector&lt;double&gt; v;
v.reserve(1000);
for (int i=0; i&lt;1000; ++i)
     {x = ZufaelligeDoubleZahl();
      v.push_back(x);
     }
...
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/886257</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/886257</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Thu, 06 Oct 2005 09:32:28 GMT</pubDate></item><item><title><![CDATA[Reply to vector effizient füllen on Thu, 06 Oct 2005 18:24:19 GMT]]></title><description><![CDATA[<p>hi, ich weiß net, ob das mit dem iterator schneller geht.</p>
<pre><code class="language-cpp">vector&lt;double&gt;d(1000);
int i=0;
for(vector&lt;double&gt;::iterator it=d.begin();it&lt;d.end();++it)
	(*it)=i++;
</code></pre>
<p>sonst wüsst ich auch net, was die schnellste möglichkeit ist.<br />
aber ich denke, dass es bei 1000 werten noch relativ uninteressant ist, welche möglichkeit du nimmst ;).. erst bei &gt;10^10 werten wird es sich vll doch etwas in der zeit niederschlagen <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>mfg mika</p>
]]></description><link>https://www.c-plusplus.net/forum/post/886633</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/886633</guid><dc:creator><![CDATA[_mika_]]></dc:creator><pubDate>Thu, 06 Oct 2005 18:24:19 GMT</pubDate></item></channel></rss>