<?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 &#x2F; shuffle &#x2F; iteratoren]]></title><description><![CDATA[<p>Hallo ich möchte eine Funktion schreiben,<br />
welche die elemente eines vectors zufällig mischt.</p>
<p>dazu möchte ich einen iterator welcher auf den beginn<br />
und einen welcher auf das ende zeigt der funktion übergeben.</p>
<p>und danach jedes element mit einem im noch unbearbeiteten bereich<br />
vertauschen.</p>
<p>wie kann ich die länge des vectors bestimmen, wenn ich nur die genannten<br />
iteratoren übergebe?</p>
<p>oder wie kann ich die aktuelle position eines iteratoras abfragen und wie setzen?</p>
<p>danke</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/96640/vector-shuffle-iteratoren</link><generator>RSS for Node</generator><lastBuildDate>Thu, 02 Jul 2026 15:26:39 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/96640.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 03 Jan 2005 21:01:59 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to vector &#x2F; shuffle &#x2F; iteratoren on Mon, 03 Jan 2005 21:01:59 GMT]]></title><description><![CDATA[<p>Hallo ich möchte eine Funktion schreiben,<br />
welche die elemente eines vectors zufällig mischt.</p>
<p>dazu möchte ich einen iterator welcher auf den beginn<br />
und einen welcher auf das ende zeigt der funktion übergeben.</p>
<p>und danach jedes element mit einem im noch unbearbeiteten bereich<br />
vertauschen.</p>
<p>wie kann ich die länge des vectors bestimmen, wenn ich nur die genannten<br />
iteratoren übergebe?</p>
<p>oder wie kann ich die aktuelle position eines iteratoras abfragen und wie setzen?</p>
<p>danke</p>
]]></description><link>https://www.c-plusplus.net/forum/post/686138</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/686138</guid><dc:creator><![CDATA[mr.delete]]></dc:creator><pubDate>Mon, 03 Jan 2005 21:01:59 GMT</pubDate></item><item><title><![CDATA[Reply to vector &#x2F; shuffle &#x2F; iteratoren on Mon, 03 Jan 2005 21:09:04 GMT]]></title><description><![CDATA[<p>Die bereits vorhandene Funktion std::random_shuffle(...) möchtest du<br />
nicht verwenden ??</p>
<p>mfg JJ</p>
]]></description><link>https://www.c-plusplus.net/forum/post/686153</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/686153</guid><dc:creator><![CDATA[John Doe]]></dc:creator><pubDate>Mon, 03 Jan 2005 21:09:04 GMT</pubDate></item><item><title><![CDATA[Reply to vector &#x2F; shuffle &#x2F; iteratoren on Mon, 03 Jan 2005 21:11:43 GMT]]></title><description><![CDATA[<p>die länge des vectors kannst du ermitteln, indem du in einer for-schleife vom anfang bis zum ende mit dem iterator durchläufst und dann einen counter hochzählst... oder du nimmst einfach:</p>
<pre><code class="language-cpp">int laenge = vectorname.size();
</code></pre>
<p>aber warum übergibst du die iteratoren und nicht einfach den vector selber? das wäre doch viel leichter und nicht so umständlich...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/686157</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/686157</guid><dc:creator><![CDATA[leech]]></dc:creator><pubDate>Mon, 03 Jan 2005 21:11:43 GMT</pubDate></item><item><title><![CDATA[Reply to vector &#x2F; shuffle &#x2F; iteratoren on Mon, 03 Jan 2005 21:14:19 GMT]]></title><description><![CDATA[<p>weil die aufgabe so gestellt wurde!<br />
ich weiss es gebe diverse einfachere lösungsansätze,<br />
aber die aufgabenstellung verlangt eine funktion, welche einen vector mischt,<br />
der funktion werden aber nur start und end iterator übergeben.</p>
<p>gibt es keine -unter dieser einschränkenden bedingung - elegantere lösung<br />
als das durchzählen mit einem counter?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/686163</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/686163</guid><dc:creator><![CDATA[mr.delete]]></dc:creator><pubDate>Mon, 03 Jan 2005 21:14:19 GMT</pubDate></item><item><title><![CDATA[Reply to vector &#x2F; shuffle &#x2F; iteratoren on Mon, 03 Jan 2005 21:19:07 GMT]]></title><description><![CDATA[<p>Nun, du könntest dir, zur Inspiration, ja mal die &quot;Musterlösung&quot; in<br />
random_shuffle ankucken. <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>
<p>mfg JJ</p>
]]></description><link>https://www.c-plusplus.net/forum/post/686169</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/686169</guid><dc:creator><![CDATA[John Doe]]></dc:creator><pubDate>Mon, 03 Jan 2005 21:19:07 GMT</pubDate></item><item><title><![CDATA[Reply to vector &#x2F; shuffle &#x2F; iteratoren on Mon, 03 Jan 2005 21:41:01 GMT]]></title><description><![CDATA[<p>wo finde ich die?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/686192</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/686192</guid><dc:creator><![CDATA[mr.delete]]></dc:creator><pubDate>Mon, 03 Jan 2005 21:41:01 GMT</pubDate></item><item><title><![CDATA[Reply to vector &#x2F; shuffle &#x2F; iteratoren on Mon, 03 Jan 2005 21:44:01 GMT]]></title><description><![CDATA[<p>in &lt;algorithm&gt;<br />
(im include verzeichnis deines compilers <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>
<p>mfg</p>
]]></description><link>https://www.c-plusplus.net/forum/post/686196</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/686196</guid><dc:creator><![CDATA[terraner]]></dc:creator><pubDate>Mon, 03 Jan 2005 21:44:01 GMT</pubDate></item><item><title><![CDATA[Reply to vector &#x2F; shuffle &#x2F; iteratoren on Mon, 03 Jan 2005 22:00:21 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">// random_shuffle

template &lt;class _RandomAccessIter&gt;
inline void random_shuffle(_RandomAccessIter __first,
                           _RandomAccessIter __last) {
  if (__first == __last) return;
  for (_RandomAccessIter __i = __first + 1; __i != __last; ++__i)
    iter_swap(__i, __first + __random_number((__i - __first) + 1));
}
</code></pre>
<p>recht elegant, obwhol ich es so nicht ganz verstehe und nicht direkt verwenden kann....hmmm</p>
]]></description><link>https://www.c-plusplus.net/forum/post/686209</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/686209</guid><dc:creator><![CDATA[mr.delete]]></dc:creator><pubDate>Mon, 03 Jan 2005 22:00:21 GMT</pubDate></item><item><title><![CDATA[Reply to vector &#x2F; shuffle &#x2F; iteratoren on Tue, 03 Feb 2015 17:10:44 GMT]]></title><description><![CDATA[<p>wie funktioniert der algo?</p>
<p>kann man auch folgendes implementieren? ich weiss, der algo ist recht schlecht von der laufzeit...</p>
<pre><code>vector&lt;int&gt; vec = {1, 2, 3, 4, 5, 6, 7};

void shuffle(vector&lt;int&gt; &amp;vec) {
	vector&lt;int&gt; shuffled(vec.size());
	int index = 0;

	while (index &lt; shuffled.size()) {
		int r = rand() % vec.size();

		if (vec[r] != -1) {
			shuffled[index] = vec[r];
			vec[r] = -1;
			index++;
		}
	}

	std::copy(shuffled.begin(). shuffled.end(), vec.begin());
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2440982</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2440982</guid><dc:creator><![CDATA[coder007]]></dc:creator><pubDate>Tue, 03 Feb 2015 17:10:44 GMT</pubDate></item><item><title><![CDATA[Reply to vector &#x2F; shuffle &#x2F; iteratoren on Tue, 03 Feb 2015 17:22:36 GMT]]></title><description><![CDATA[<p>coder007 schrieb:</p>
<blockquote>
<p>kann man auch folgendes implementieren? ich weiss, der algo ist recht schlecht von der laufzeit...</p>
</blockquote>
<p>Vorausgesetzt, das Eingabearray hat keine -1, dann produziert dieser Algorithmus eine zufällige Permutation. Insofern ist er &quot;korrekt&quot;.</p>
<p>Aber langsam, unelegant und ungenerisch.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2440984</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2440984</guid><dc:creator><![CDATA[schaffler]]></dc:creator><pubDate>Tue, 03 Feb 2015 17:22:36 GMT</pubDate></item><item><title><![CDATA[Reply to vector &#x2F; shuffle &#x2F; iteratoren on Tue, 03 Feb 2015 17:28:05 GMT]]></title><description><![CDATA[<p>das stimmt, was wäre die nächst schnellere variante?</p>
<p>wie funktioniert dieser random_shuffle algo aus dem vorherigen post?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2440985</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2440985</guid><dc:creator><![CDATA[coder007]]></dc:creator><pubDate>Tue, 03 Feb 2015 17:28:05 GMT</pubDate></item><item><title><![CDATA[Reply to vector &#x2F; shuffle &#x2F; iteratoren on Tue, 03 Feb 2015 17:45:10 GMT]]></title><description><![CDATA[<p>Hier eine lesbarere Implementierung (von <a href="http://en.cppreference.com/w/cpp/algorithm/random_shuffle" rel="nofollow">http://en.cppreference.com/w/cpp/algorithm/random_shuffle</a>):</p>
<pre><code>template&lt; class RandomIt &gt;
void random_shuffle( RandomIt first, RandomIt last )
{
    typename std::iterator_traits&lt;RandomIt&gt;::difference_type i, n;
    n = last - first;
    for (i = n-1; i &gt; 0; --i) {
        using std::swap;
        swap(first[i], first[std::rand() % (i+1)]);
    }
}
</code></pre>
<p>(diese Version ist deprecated, weil sie rand() nutzt, in C++11 gibt es wesentlich bessere Generatoren)<br />
Sie läuft einfach in einer Schleife von n-1 bis 0 und swappt das i-te Element mit einem Element aus dem Intervall [0, i].</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2440987</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2440987</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Tue, 03 Feb 2015 17:45:10 GMT</pubDate></item><item><title><![CDATA[Reply to vector &#x2F; shuffle &#x2F; iteratoren on Tue, 03 Feb 2015 17:50:57 GMT]]></title><description><![CDATA[<p>Nathan schrieb:</p>
<blockquote>
<p>Sie läuft einfach in einer Schleife von n-1 bis 0 und swappt das i-te Element mit einem Element aus dem Intervall [0, i].</p>
</blockquote>
<p>Hier der Link zur Erklärung, warum das funktioniert: <a href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle" rel="nofollow">https://en.wikipedia.org/wiki/Fisher–Yates_shuffle</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2440990</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2440990</guid><dc:creator><![CDATA[Induktionsherd]]></dc:creator><pubDate>Tue, 03 Feb 2015 17:50:57 GMT</pubDate></item><item><title><![CDATA[Reply to vector &#x2F; shuffle &#x2F; iteratoren on Tue, 03 Feb 2015 19:11:56 GMT]]></title><description><![CDATA[<p>welche laufzeit komplexitaet hat dieser algo?</p>
<pre><code>void shuffle(vector&lt;int&gt; &amp;vec) { 
    vector&lt;int&gt; shuffled(vec.size()); 
    int index = 0; 

    while (index &lt; shuffled.size()) { 
        int r = rand() % vec.size(); 

        if (vec[r] != -1) { 
            shuffled[index] = vec[r]; 
            vec[r] = -1; 
            index++; 
        } 
    } 

    std::copy(shuffled.begin(). shuffled.end(), vec.begin()); 
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2441006</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2441006</guid><dc:creator><![CDATA[coder007]]></dc:creator><pubDate>Tue, 03 Feb 2015 19:11:56 GMT</pubDate></item><item><title><![CDATA[Reply to vector &#x2F; shuffle &#x2F; iteratoren on Tue, 03 Feb 2015 22:27:28 GMT]]></title><description><![CDATA[<p>O(n)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2441058</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2441058</guid><dc:creator><![CDATA[Skym0sh0]]></dc:creator><pubDate>Tue, 03 Feb 2015 22:27:28 GMT</pubDate></item><item><title><![CDATA[Reply to vector &#x2F; shuffle &#x2F; iteratoren on Tue, 03 Feb 2015 22:40:47 GMT]]></title><description><![CDATA[<p>Skym0sh0 schrieb:</p>
<blockquote>
<p>O(n)</p>
</blockquote>
<p>Mit der O-Notation kann man hier nicht argumentieren, Worst Case ist Endlosschleife.</p>
<p>Der i-te Schritt braucht etwa n/(n-i) Schritte. Nimmt man die Summe hat man n*(1/1+1/2+1/3+...+1/n), was n*<a href="https://en.wikipedia.org/wiki/Harmonic_number" rel="nofollow">Hn</a>≈n log n entspricht.</p>
<p>Also durchschnittlich quasilinear Laufzeit.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2441066</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2441066</guid><dc:creator><![CDATA[Induktionsherd]]></dc:creator><pubDate>Tue, 03 Feb 2015 22:40:47 GMT</pubDate></item><item><title><![CDATA[Reply to vector &#x2F; shuffle &#x2F; iteratoren on Tue, 03 Feb 2015 22:42:00 GMT]]></title><description><![CDATA[<p>SeppJ schrieb:</p>
<blockquote>
<p>Skym0sh0 schrieb:</p>
<blockquote>
<p>O(n)</p>
</blockquote>
<p>Nein, das ist O(n^2). Die Schleife läuft im Schnitt so viele Schritte wie die Summe der ganzen Zahlen von 1 bis n.</p>
</blockquote>
<p>Er nimmt sich einen zufälligen Index, er läuft nicht von vorne nach hinten durch. Das n/2-te Element braucht nur etwa 2 Versuche, da 50% aller Zufallsindices noch voll sind.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2441067</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2441067</guid><dc:creator><![CDATA[Induktionsherd]]></dc:creator><pubDate>Tue, 03 Feb 2015 22:42:00 GMT</pubDate></item><item><title><![CDATA[Reply to vector &#x2F; shuffle &#x2F; iteratoren on Tue, 03 Feb 2015 22:44:34 GMT]]></title><description><![CDATA[<p>Induktionsherd schrieb:</p>
<blockquote>
<p>SeppJ schrieb:</p>
<blockquote>
<p>Skym0sh0 schrieb:</p>
<blockquote>
<p>O(n)</p>
</blockquote>
<p>Nein, das ist O(n^2). Die Schleife läuft im Schnitt so viele Schritte wie die Summe der ganzen Zahlen von 1 bis n.</p>
</blockquote>
<p>Er nimmt sich einen zufälligen Index, er läuft nicht von vorne nach hinten durch. Das n/2-te Element braucht nur etwa 2 Versuche, da 50% aller Zufallsindices noch voll sind.</p>
</blockquote>
<p>Hast recht, das fiel mir wenige Sekunden nach dem Posten auch selber auf und ich habe den Beitrag daher wieder gelöscht, da zu dem Zeitpunkt noch keine andere Antwort da war. Da warst du anscheinend wirklich in diesem 20 Sekundenfenster aktiv, als meine Antwort so im Forum stand.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2441068</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2441068</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Tue, 03 Feb 2015 22:44:34 GMT</pubDate></item><item><title><![CDATA[Reply to vector &#x2F; shuffle &#x2F; iteratoren on Wed, 04 Feb 2015 13:57:49 GMT]]></title><description><![CDATA[<p>Induktionsherd schrieb:</p>
<blockquote>
<p>Skym0sh0 schrieb:</p>
<blockquote>
<p>O(n)</p>
</blockquote>
<p>Mit der O-Notation kann man hier nicht argumentieren, Worst Case ist Endlosschleife.</p>
<p>Der i-te Schritt braucht etwa n/(n-i) Schritte. Nimmt man die Summe hat man n*(1/1+1/2+1/3+...+1/n), was n*<a href="https://en.wikipedia.org/wiki/Harmonic_number" rel="nofollow">Hn</a>≈n log n entspricht.</p>
<p>Also durchschnittlich quasilinear Laufzeit.</p>
</blockquote>
<p>Ups, mist übersehen.</p>
<p>Ja, hast Recht. Worst-Case ist echt die Endlosschleife. Mein Fehler.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2441134</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2441134</guid><dc:creator><![CDATA[Skym0sh0]]></dc:creator><pubDate>Wed, 04 Feb 2015 13:57:49 GMT</pubDate></item></channel></rss>