<?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 blockweise mischen]]></title><description><![CDATA[<p>Hey,</p>
<p>ich möchte einen std::vector gerne blockweise mischen. Das heißt, dass nicht der gesamte Vector gemischt wird, sondern immer nur ein Teil davon. Momentan habe ich das so gelöst, aber die Lösung sagt mir nicht so zu.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;ctime&gt;
#include &lt;algorithm&gt;

typedef std::vector&lt;int&gt; Vec;

void fillVec(Vec&amp; v, unsigned int count);
void partialShuffle(Vec&amp; v, unsigned int blockSize);

int main()
{
	Vec v;

	srand(static_cast&lt;unsigned int&gt;(time(NULL)));

	for (unsigned int i = 0; i &lt; 25; ++i)
	{
		fillVec(v, 15);

		partialShuffle(v, 4);

		for (unsigned int j = 0; j &lt; v.size(); ++j)
		{
			std::cout &lt;&lt; v[j] &lt;&lt; &quot; &quot;;
			if ((j+1) % 4 == 0 &amp;&amp; j &gt; 0) std::cout &lt;&lt; &quot; | &quot;;
		}

		std::cout &lt;&lt; &quot; = &quot; &lt;&lt; v.size() &lt;&lt; std::endl;
	}
}

void fillVec(Vec&amp; v, unsigned int count)
{
	v.clear();

	for (unsigned int i = 1; i &lt;= count; ++i)
	{
		v.push_back(i);
	}
}

void partialShuffle(Vec&amp; v, unsigned int blockSize)
{
	Vec block, res;

	if (blockSize &lt;= 0)
	{
		return;
	}

	for (unsigned int i = 0; i &lt; v.size();)
	{
		for (unsigned int j = 0; j &lt;= blockSize - 1 &amp;&amp; i &lt; v.size(); ++j, ++i)
		{
			block.push_back(v[i]);
		}

		std::random_shuffle(block.begin(), block.end());
		res.insert(res.end(), block.begin(), block.end());

		block.clear();
	}

	v = res;
}
</code></pre>
<p>Geht es einfacher und schöner?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/265627/vector-blockweise-mischen</link><generator>RSS for Node</generator><lastBuildDate>Thu, 03 Sep 2026 23:34:29 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/265627.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 26 Apr 2010 15:01:52 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Vector blockweise mischen on Mon, 26 Apr 2010 15:01:52 GMT]]></title><description><![CDATA[<p>Hey,</p>
<p>ich möchte einen std::vector gerne blockweise mischen. Das heißt, dass nicht der gesamte Vector gemischt wird, sondern immer nur ein Teil davon. Momentan habe ich das so gelöst, aber die Lösung sagt mir nicht so zu.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;ctime&gt;
#include &lt;algorithm&gt;

typedef std::vector&lt;int&gt; Vec;

void fillVec(Vec&amp; v, unsigned int count);
void partialShuffle(Vec&amp; v, unsigned int blockSize);

int main()
{
	Vec v;

	srand(static_cast&lt;unsigned int&gt;(time(NULL)));

	for (unsigned int i = 0; i &lt; 25; ++i)
	{
		fillVec(v, 15);

		partialShuffle(v, 4);

		for (unsigned int j = 0; j &lt; v.size(); ++j)
		{
			std::cout &lt;&lt; v[j] &lt;&lt; &quot; &quot;;
			if ((j+1) % 4 == 0 &amp;&amp; j &gt; 0) std::cout &lt;&lt; &quot; | &quot;;
		}

		std::cout &lt;&lt; &quot; = &quot; &lt;&lt; v.size() &lt;&lt; std::endl;
	}
}

void fillVec(Vec&amp; v, unsigned int count)
{
	v.clear();

	for (unsigned int i = 1; i &lt;= count; ++i)
	{
		v.push_back(i);
	}
}

void partialShuffle(Vec&amp; v, unsigned int blockSize)
{
	Vec block, res;

	if (blockSize &lt;= 0)
	{
		return;
	}

	for (unsigned int i = 0; i &lt; v.size();)
	{
		for (unsigned int j = 0; j &lt;= blockSize - 1 &amp;&amp; i &lt; v.size(); ++j, ++i)
		{
			block.push_back(v[i]);
		}

		std::random_shuffle(block.begin(), block.end());
		res.insert(res.end(), block.begin(), block.end());

		block.clear();
	}

	v = res;
}
</code></pre>
<p>Geht es einfacher und schöner?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1888517</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1888517</guid><dc:creator><![CDATA[jkljkl]]></dc:creator><pubDate>Mon, 26 Apr 2010 15:01:52 GMT</pubDate></item><item><title><![CDATA[Reply to Vector blockweise mischen on Mon, 26 Apr 2010 15:08:08 GMT]]></title><description><![CDATA[<p>Wozu selber machen, was schon in der Standardbibliothek ist?</p>
<p><a href="http://www.cplusplus.com/reference/algorithm/random_shuffle/" rel="nofollow">Blockweises Mischen</a></p>
<p>edit: Ups, du hast random_shuffle ja schon benutzt. Warum denn dann so kompliziert und nicht einfach auf den zu mischenden Block anwenden?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1888521</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1888521</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Mon, 26 Apr 2010 15:08:08 GMT</pubDate></item><item><title><![CDATA[Reply to Vector blockweise mischen on Mon, 26 Apr 2010 15:14:32 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/19375">@SeppJ</a>,<br />
Er benutzt ja schon <code>random_shuffle</code> nur unheimlich umständlich.</p>
<pre><code class="language-cpp">void partial_shuffle(std::vector&lt;int&gt;&amp; vec, std::size_t blockSize)
{
  if(blockSize == 0)
  {
    return;
  }

  if(blockSize == 1)
  {
    std::random_shuffle(vec.begin(), vec.end());
  }

  std::size_t vecSize = vec.size();
  std::vector&lt;int&gt;::iterator first = vec.begin();

  while(vecSize &gt;= blockSize)
  {
    std::vector&lt;int&gt;::iterator last = first;
    std::advance(last, blockSize);

    std::random_shuffle(first, last);

    first = last;
    vecSize -= blockSize;
  }

  std::random_shuffle(first, vec.end());
}
</code></pre>
<p>Ungetestet, einfach schnell aus dem Kopf raus.</p>
<p>Grüssli</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1888524</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1888524</guid><dc:creator><![CDATA[Dravere]]></dc:creator><pubDate>Mon, 26 Apr 2010 15:14:32 GMT</pubDate></item><item><title><![CDATA[Reply to Vector blockweise mischen on Mon, 26 Apr 2010 16:21:31 GMT]]></title><description><![CDATA[<p>Dravere schrieb:</p>
<blockquote>
<pre><code class="language-cpp">void partial_shuffle(std::vector&lt;int&gt;&amp; vec, std::size_t blockSize)
{
  if(blockSize == 0)
  {
    return;
  }
  
  if(blockSize == 1)
  {
    std::random_shuffle(vec.begin(), vec.end());
  }
</code></pre>
</blockquote>
<p>Bei blockSize==1 gibt's aber nichts zu tun. Du machst aus blockSize==1 einfach blockSize==vec.size().</p>
<p>kk</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1888556</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1888556</guid><dc:creator><![CDATA[krümelkacker]]></dc:creator><pubDate>Mon, 26 Apr 2010 16:21:31 GMT</pubDate></item><item><title><![CDATA[Reply to Vector blockweise mischen on Mon, 26 Apr 2010 16:33:26 GMT]]></title><description><![CDATA[<p>krümelkacker schrieb:</p>
<blockquote>
<p>Bei blockSize==1 gibt's aber nichts zu tun. Du machst aus blockSize==1 einfach blockSize==vec.size().</p>
</blockquote>
<p>Jap, stimmt. Denkfehler gemacht. War wie gesagt, ganz schnell hingeschrieben. Sonstige Fehler? <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>Grüssli</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1888563</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1888563</guid><dc:creator><![CDATA[Dravere]]></dc:creator><pubDate>Mon, 26 Apr 2010 16:33:26 GMT</pubDate></item><item><title><![CDATA[Reply to Vector blockweise mischen on Mon, 26 Apr 2010 18:59:21 GMT]]></title><description><![CDATA[<p>Danke für eure Antworten. Was haltet ihr von meiner neuen Lösung:</p>
<pre><code class="language-cpp">void partialShuffle(Vec&amp; v, unsigned int blockSize)
{
   if (blockSize &lt;= 1)
   {
	   return;
   }

   int limit = (v.size() - (v.size() % blockSize)) / blockSize;

   for (Vec::iterator it = v.begin(); it != v.end(); --limit)
   {
	   std::random_shuffle(it, it + blockSize);

	   it += blockSize;

	   if (limit &lt;= 1)
	   {
		   blockSize = v.size() % blockSize;
	   }
   }
}
</code></pre>
<p>Müsste doch korrekt sein, oder?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1888638</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1888638</guid><dc:creator><![CDATA[jkljkl]]></dc:creator><pubDate>Mon, 26 Apr 2010 18:59:21 GMT</pubDate></item><item><title><![CDATA[Reply to Vector blockweise mischen on Mon, 26 Apr 2010 19:19:40 GMT]]></title><description><![CDATA[<p>Version von Dravere ein wenig verändert (ungetestet):</p>
<pre><code class="language-cpp">void partial_shuffle(std::vector&lt;int&gt;&amp; vec, std::size_t blockSize)
{
  std::vector&lt;int&gt; shuffledEnd=vec.begin();
  while(std::size_t toDo=std::min(vec.end()-shuffledEnd,blocksize))
  {
    std::random_shuffle(shuffledEnd,shuffledEnd+toDo);
    shuffledEnd+=toDo;
  }
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1888652</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1888652</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Mon, 26 Apr 2010 19:19:40 GMT</pubDate></item><item><title><![CDATA[Reply to Vector blockweise mischen on Mon, 26 Apr 2010 20:21:00 GMT]]></title><description><![CDATA[<p>In der 3. Zeile fehlt noch nen ::iterator<br />
<img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61b.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_tongue"
      title=":P"
      alt="😛"
    /></p>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1888684</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1888684</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Mon, 26 Apr 2010 20:21:00 GMT</pubDate></item></channel></rss>