<?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[Hilfe zum Verständnis von Vectoren]]></title><description><![CDATA[<p>Hallo,<br />
Ich möchte einen Würfel 10x werfen und mir in einem Vector notieren wie oft eine Augenzahl vorkommt.</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;vector&gt;
using namespace std;

int main(){

	int counter = 0;
	std::vector&lt;int&gt; x;
	for (counter; counter &lt; 10; counter++){
		unsigned int die1 = 1 + rand() % 6; // rolling a dice 10 times
		switch (die1){
		case 1: x.push_back(0); break; //Storing how often a number is rolled
		case 2: x.push_back(1); break; 
		case 3: x.push_back(2); break;
		case 4: x.push_back(3); break;
		case 5: x.push_back(4); break;
		case 6: x.push_back(5); break;
		default: cout &lt;&lt; &quot;Something went wrong&quot;; break;
		}

	} // end of for

	cout &lt;&lt; &quot;1 was rolled: &quot; &lt;&lt; x[0] &lt;&lt; &quot; times&quot; &lt;&lt; endl
		&lt;&lt; &quot;2 was rolled: &quot; &lt;&lt; x[1] &lt;&lt; &quot; times&quot; &lt;&lt; endl
		&lt;&lt; &quot;3 was rolled: &quot; &lt;&lt; x[2] &lt;&lt; &quot; times&quot; &lt;&lt; endl
		&lt;&lt; &quot;4 was rolled: &quot; &lt;&lt; x[3] &lt;&lt; &quot; times&quot; &lt;&lt; endl
		&lt;&lt; &quot;5 was rolled: &quot; &lt;&lt; x[4] &lt;&lt; &quot; times&quot; &lt;&lt; endl
		&lt;&lt; &quot;6 was rolled: &quot; &lt;&lt; x[5] &lt;&lt; &quot; times&quot; &lt;&lt; endl;

	system(&quot;PAUSE&quot;);
	return 0;
}
</code></pre>
<p>Das ist mein Code, ich weiß das ich bei push_back ein neues Element im Vektor erstelle und meine Ausgabe somit falsch ist weil sie die Augenzahl in Wurf X ausgibt.</p>
<p>Wie kann ich also einen Wert in ein schon vorhandenes Element addieren?<br />
mfg</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/325793/hilfe-zum-verständnis-von-vectoren</link><generator>RSS for Node</generator><lastBuildDate>Sat, 11 Jul 2026 19:42:04 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/325793.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 18 May 2014 16:34:55 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Hilfe zum Verständnis von Vectoren on Sun, 18 May 2014 16:34:55 GMT]]></title><description><![CDATA[<p>Hallo,<br />
Ich möchte einen Würfel 10x werfen und mir in einem Vector notieren wie oft eine Augenzahl vorkommt.</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;vector&gt;
using namespace std;

int main(){

	int counter = 0;
	std::vector&lt;int&gt; x;
	for (counter; counter &lt; 10; counter++){
		unsigned int die1 = 1 + rand() % 6; // rolling a dice 10 times
		switch (die1){
		case 1: x.push_back(0); break; //Storing how often a number is rolled
		case 2: x.push_back(1); break; 
		case 3: x.push_back(2); break;
		case 4: x.push_back(3); break;
		case 5: x.push_back(4); break;
		case 6: x.push_back(5); break;
		default: cout &lt;&lt; &quot;Something went wrong&quot;; break;
		}

	} // end of for

	cout &lt;&lt; &quot;1 was rolled: &quot; &lt;&lt; x[0] &lt;&lt; &quot; times&quot; &lt;&lt; endl
		&lt;&lt; &quot;2 was rolled: &quot; &lt;&lt; x[1] &lt;&lt; &quot; times&quot; &lt;&lt; endl
		&lt;&lt; &quot;3 was rolled: &quot; &lt;&lt; x[2] &lt;&lt; &quot; times&quot; &lt;&lt; endl
		&lt;&lt; &quot;4 was rolled: &quot; &lt;&lt; x[3] &lt;&lt; &quot; times&quot; &lt;&lt; endl
		&lt;&lt; &quot;5 was rolled: &quot; &lt;&lt; x[4] &lt;&lt; &quot; times&quot; &lt;&lt; endl
		&lt;&lt; &quot;6 was rolled: &quot; &lt;&lt; x[5] &lt;&lt; &quot; times&quot; &lt;&lt; endl;

	system(&quot;PAUSE&quot;);
	return 0;
}
</code></pre>
<p>Das ist mein Code, ich weiß das ich bei push_back ein neues Element im Vektor erstelle und meine Ausgabe somit falsch ist weil sie die Augenzahl in Wurf X ausgibt.</p>
<p>Wie kann ich also einen Wert in ein schon vorhandenes Element addieren?<br />
mfg</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2399761</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2399761</guid><dc:creator><![CDATA[Pantoffeltier]]></dc:creator><pubDate>Sun, 18 May 2014 16:34:55 GMT</pubDate></item><item><title><![CDATA[Reply to Hilfe zum Verständnis von Vectoren on Sun, 18 May 2014 16:43:19 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich würde sagen, dass du einen Vektor mit 6 Elementen anlegst und für die gewürfelte Augenzahl einfach sagst.</p>
<pre><code class="language-cpp">std::vector&lt;int&gt; vec( 6, 0);

int augenzahl = ... // von 1 bis 6

vec[ augenzahl - 1]++;
</code></pre>
<p>Edit:<br />
Ich empfehle dir die C++11 Zufallszahlengeneratoren zur Simulation von Würfel, z.B. <a href="http://www.cplusplus.com/reference/random/uniform_int_distribution/" rel="nofollow">das hier</a>.</p>
<p>Gruß,<br />
-- Klaus.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2399762</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2399762</guid><dc:creator><![CDATA[Klaus82]]></dc:creator><pubDate>Sun, 18 May 2014 16:43:19 GMT</pubDate></item><item><title><![CDATA[Reply to Hilfe zum Verständnis von Vectoren on Sun, 18 May 2014 16:46:33 GMT]]></title><description><![CDATA[<p>Statt <em>vector</em> würde ich <em>std::array&lt;int, 6&gt; count{}</em> empfehlen. Dann kannst du dir per <em>count[die1-1]++;</em> merken wann immer ein Wurf kam (was aber auch mit <em>vector</em> geht). <em>rand()</em> sollte mit <em>srand</em> mit einer Zufallszahl initialisiert werden, obwohl <em>rand</em> <a href="http://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful" rel="nofollow">generell nicht so super ist</a>.<br />
Außerdem kannst du Werte im Code direkt nutzen. Statt <em>switch (die1) x.push_back</em> kannst du direkt <em>x.push_back(die1-1);</em> schreiben. Statt der ganzen cout-Anweisungen geht da auch eine Schleife von 1 bis 6.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2399764</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2399764</guid><dc:creator><![CDATA[nwp3]]></dc:creator><pubDate>Sun, 18 May 2014 16:46:33 GMT</pubDate></item></channel></rss>