<?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[Felder als STL Container]]></title><description><![CDATA[<p>hey,<br />
ich soll Felder sortieren mit versch. Sortieralgorithmen (bubblesort u.ä.).<br />
soll für die Felder den STL-Container vector aus der Standard Template Library STL verwenden.</p>
<p>inwiefern kann ich ein &quot;normales&quot; array, z.B.</p>
<pre><code>const int l = 10; //Größe des Arrays
	int A[l] = { 39, 12, 72, 11, 18, 46, 83, 21, 10, 1 };
</code></pre>
<p>in einen STL-Container umwandeln?<br />
wie include ich die STL? bestimmter befehl?</p>
<p>zudem sieht einer meiner sortieralgorithemen immoment so aus:</p>
<pre><code>void bubbleSort(int my_array[], int l) {

	bool vertauscht = true;

	int j = 0;

	int tmp;

	while (vertauscht) {

		vertauscht = false;

		j++;

		for (int i = 0; i &lt; l - j; i++) {

			if (my_array[i] &gt; my_array[i + 1]) {

				tmp = my_array[i];

				my_array[i] = my_array[i + 1];

				my_array[i + 1] = tmp;

				vertauscht = true;
			}
		}
	}
}
</code></pre>
<p>muss ich meinen sortiercode verändern wenn ich stl-container statt arrays verwende?<br />
wenn ja: wie?</p>
<p>vielen dank für hilfe</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/332540/felder-als-stl-container</link><generator>RSS for Node</generator><lastBuildDate>Mon, 27 Apr 2026 19:16:54 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/332540.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 07 May 2015 09:38:19 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Felder als STL Container on Thu, 07 May 2015 09:38:19 GMT]]></title><description><![CDATA[<p>hey,<br />
ich soll Felder sortieren mit versch. Sortieralgorithmen (bubblesort u.ä.).<br />
soll für die Felder den STL-Container vector aus der Standard Template Library STL verwenden.</p>
<p>inwiefern kann ich ein &quot;normales&quot; array, z.B.</p>
<pre><code>const int l = 10; //Größe des Arrays
	int A[l] = { 39, 12, 72, 11, 18, 46, 83, 21, 10, 1 };
</code></pre>
<p>in einen STL-Container umwandeln?<br />
wie include ich die STL? bestimmter befehl?</p>
<p>zudem sieht einer meiner sortieralgorithemen immoment so aus:</p>
<pre><code>void bubbleSort(int my_array[], int l) {

	bool vertauscht = true;

	int j = 0;

	int tmp;

	while (vertauscht) {

		vertauscht = false;

		j++;

		for (int i = 0; i &lt; l - j; i++) {

			if (my_array[i] &gt; my_array[i + 1]) {

				tmp = my_array[i];

				my_array[i] = my_array[i + 1];

				my_array[i + 1] = tmp;

				vertauscht = true;
			}
		}
	}
}
</code></pre>
<p>muss ich meinen sortiercode verändern wenn ich stl-container statt arrays verwende?<br />
wenn ja: wie?</p>
<p>vielen dank für hilfe</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2452828</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2452828</guid><dc:creator><![CDATA[Dangling]]></dc:creator><pubDate>Thu, 07 May 2015 09:38:19 GMT</pubDate></item><item><title><![CDATA[Reply to Felder als STL Container on Thu, 07 May 2015 09:54:59 GMT]]></title><description><![CDATA[<p>Annahme: dein Compiler kann halbwegs C++11 (z.B. VS2013 für Windows besser VS2015)</p>
<p>Dangling schrieb:</p>
<blockquote>
<p>inwiefern kann ich ein &quot;normales&quot; array, z.B.</p>
<pre><code>const int l = 10; //Größe des Arrays
	int A[l] = { 39, 12, 72, 11, 18, 46, 83, 21, 10, 1 };
</code></pre>
<p>in einen STL-Container umwandeln?<br />
wie include ich die STL? bestimmter befehl?</p>
</blockquote>
<pre><code class="language-cpp">#include &lt;vector&gt;
....
std::vector&lt;int&gt; A = { 39, 12, 72, 11, 18, 46, 83, 21, 10, 1 };
</code></pre>
<p>Dangling schrieb:</p>
<blockquote>
<p>muss ich meinen sortiercode verändern wenn ich stl-container statt arrays verwende?<br />
wenn ja: wie?</p>
</blockquote>
<p>Du musst eine Referenz auf den Vector entgegennehmen. Der Vector kennt seine Länge =&gt; kein Längenparameter</p>
<p>Da du offensichtlich keine Ahnung bzgl. der STL hast, solltest du vorher vielleicht mal ein Buch zu Rate ziehen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2452834</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2452834</guid><dc:creator><![CDATA[manni66]]></dc:creator><pubDate>Thu, 07 May 2015 09:54:59 GMT</pubDate></item><item><title><![CDATA[Reply to Felder als STL Container on Thu, 07 May 2015 10:14:34 GMT]]></title><description><![CDATA[<p>aber wenn ich bei meinem code einfach alles von</p>
<pre><code>int A[]
</code></pre>
<p>in</p>
<pre><code>vector&lt;int&gt; A
</code></pre>
<p>umwandel (*s.u.), taucht die fehlermeldung<br />
&quot;debug assertion failed<br />
expression: vector subscript out of range&quot;</p>
<p>bsp für umwandlung:</p>
<pre><code>vector &lt;int&gt; A= { 39, 12, 72, 11, 18, 46, 83, 21, 10, 1 };

void insertionSort(vector&lt;int&gt; my_array, int l)
{
	int i, j, tmp;

	for (i = 1; i &lt; (int)my_array.size(); i++)
	{
		j = i;

		while (j &gt; 0 &amp;&amp; my_array[j - 1] &gt; my_array[j])
		{
			tmp = my_array[j];

			my_array[j] = my_array[j - 1];

			my_array[j - 1] = tmp;

			j--;

		}
	}
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2452837</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2452837</guid><dc:creator><![CDATA[Dangling]]></dc:creator><pubDate>Thu, 07 May 2015 10:14:34 GMT</pubDate></item><item><title><![CDATA[Reply to Felder als STL Container on Thu, 07 May 2015 10:24:27 GMT]]></title><description><![CDATA[<p>Dangling schrieb:</p>
<blockquote>
<p>aber wenn ich bei meinem code einfach alles von</p>
<pre><code>int A[]
</code></pre>
<p>in</p>
<pre><code>vector&lt;int&gt; A
</code></pre>
<p>umwandel (*s.u.), taucht die fehlermeldung<br />
&quot;debug assertion failed<br />
expression: vector subscript out of range&quot;</p>
</blockquote>
<p>Dein Algorithmus ist fehlerhaft. Mit einem Array hättest du das nicht bemerkt.</p>
<p>Unabhängig davon nochmal zur Wiederholung:<br />
Du musst eine <strong>Referenz</strong> auf den Vector entgegennehmen. Der Vector kennt seine Länge =&gt; <strong>kein Längenparameter</strong></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2452841</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2452841</guid><dc:creator><![CDATA[manni66]]></dc:creator><pubDate>Thu, 07 May 2015 10:24:27 GMT</pubDate></item><item><title><![CDATA[Reply to Felder als STL Container on Thu, 07 May 2015 10:25:24 GMT]]></title><description><![CDATA[<p>Und informier dich mal über Iteratoren, dann kannst du den Sortieralgorithmus für verschiedene Container (als auch Arrays) benutzen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2452842</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2452842</guid><dc:creator><![CDATA[Th69]]></dc:creator><pubDate>Thu, 07 May 2015 10:25:24 GMT</pubDate></item><item><title><![CDATA[Reply to Felder als STL Container on Thu, 07 May 2015 10:50:46 GMT]]></title><description><![CDATA[<p>alles klar.<br />
gebe die vektoren nun als refernz an die funktionen.<br />
und so klappt alles auch,<br />
außer bei meinem mergesort algorithmus (bei dem taucht immer noch die &quot;out of range&quot; fehlermeldung auf!):</p>
<pre><code>//sortieralgorithmus:

void merge(vector&lt;int&gt; &amp;my_array, vector &lt;int&gt; &amp;tmp_array, int leftPos, int rightPos, int rightEnd)
{
	int leftEnd = rightPos - 1;
	int tmpPos = leftPos;
	int numElements = rightEnd - leftPos + 1;

	while ((leftPos &lt;= leftEnd) &amp;&amp; (rightPos &lt;= rightEnd))
	{
		if (my_array[leftPos] &lt;= my_array[rightPos])
		{
			tmp_array[tmpPos] = my_array[leftPos];
			tmpPos = tmpPos + 1;
			leftPos = leftPos + 1;
		}
		else
		{
			tmp_array[tmpPos] = my_array[rightPos];
			tmpPos = tmpPos + 1;
			rightPos = rightPos + 1;
		}
	}

	while (leftPos &lt;= leftEnd)
	{
		tmp_array[tmpPos] = my_array[leftPos];
		tmpPos = tmpPos + 1;
		leftPos = leftPos + 1;
	}

	while (rightPos &lt;= rightEnd)
	{
		tmp_array[tmpPos] = my_array[rightPos];
		tmpPos = tmpPos + 1;
		rightPos = rightPos + 1;
	}

	int i = 0;

	while (i &lt; numElements)
	{
		my_array[rightEnd] = tmp_array[rightEnd];
		i = i + 1;
		rightEnd = rightEnd - 1;
	}
}

void mergeSort(vector&lt;int&gt; &amp;my_array, vector &lt;int&gt; &amp;tmparray, int left, int right)
{
	if (left &lt; right)
	{
		int center = (left+right) / 2;

		mergeSort(my_array, tmparray, left, center);
		mergeSort(my_array, tmparray, center + 1, right);
		merge(my_array, tmparray, left, center + 1, right);
	}
}

//in der main()
vector &lt;int&gt; A= { 39, 12, 72, 11, 18, 46, 83, 21, 10, 1 };
vector &lt;int&gt; mergearray;

mergeSort(tmp_A, mergearray, 0, (int)tmp_A.size() - 1);
</code></pre>
<p>finde nicht den fehler <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=":/"
      alt="😕"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2452845</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2452845</guid><dc:creator><![CDATA[Dangling]]></dc:creator><pubDate>Thu, 07 May 2015 10:50:46 GMT</pubDate></item><item><title><![CDATA[Reply to Felder als STL Container on Thu, 07 May 2015 10:56:27 GMT]]></title><description><![CDATA[<p>Dangling schrieb:</p>
<blockquote>
<p>außer bei meinem mergesort algorithmus (bei dem taucht immer noch die &quot;out of range&quot; fehlermeldung auf!):</p>
<pre><code>//in der main()
vector &lt;int&gt; A= { 39, 12, 72, 11, 18, 46, 83, 21, 10, 1 };
vector &lt;int&gt; mergearray;

mergeSort(tmp_A, mergearray, 0, (int)tmp_A.size() - 1);
</code></pre>
<p>finde nicht den fehler <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=":/"
      alt="😕"
    /></p>
</blockquote>
<p>Wie viele Elemente hat mergearray hier?<br />
Ist mergearray[0] ein gültiger Zugriff?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2452846</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2452846</guid><dc:creator><![CDATA[manni66]]></dc:creator><pubDate>Thu, 07 May 2015 10:56:27 GMT</pubDate></item><item><title><![CDATA[Reply to Felder als STL Container on Thu, 07 May 2015 11:13:49 GMT]]></title><description><![CDATA[<p>manni66 schrieb:</p>
<blockquote>
<p>Wie viele Elemente hat mergearray hier?<br />
Ist mergearray[0] ein gültiger Zugriff?</p>
</blockquote>
<p>zunächst ist das mergearray leer also ist es wohl kein gültiger zugriff <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=":/"
      alt="😕"
    /></p>
<p>habs nun so probiert:</p>
<pre><code>void merge(vector&lt;int&gt; &amp;my_array, vector &lt;int&gt; &amp;tmp_array, int leftPos, int rightPos, int rightEnd)
{
	int leftEnd = rightPos - 1;
	int tmpPos = leftPos;
	int numElements = rightEnd - leftPos + 1;

	while ((leftPos &lt;= leftEnd) &amp;&amp; (rightPos &lt;= rightEnd))
	{
		if (my_array[leftPos] &lt;= my_array[rightPos])
		{
			//tmp_array[tmpPos] = my_array[leftPos];
			tmp_array.push_back((int)my_array[leftPos]);
			tmpPos = tmpPos + 1;
			leftPos = leftPos + 1;
		}
		else
		{
			//tmp_array[tmpPos] = my_array[rightPos];
			tmp_array.push_back((int)my_array[rightPos]);
			tmpPos = tmpPos + 1;
			rightPos = rightPos + 1;
		}
	}

	while (leftPos &lt;= leftEnd)
	{
		//tmp_array[tmpPos] = my_array[leftPos];
		tmp_array.push_back((int)my_array[leftPos]);
		tmpPos = tmpPos + 1;
		leftPos = leftPos + 1;
	}

	while (rightPos &lt;= rightEnd)
	{
		//tmp_array[tmpPos] = my_array[rightPos];
		tmp_array.push_back((int)my_array[rightPos]);
		tmpPos = tmpPos + 1;
		rightPos = rightPos + 1;
	}

	int i = 0;

	while (i &lt; numElements)
	{
		my_array[rightEnd] = tmp_array[rightEnd];
		i = i + 1;
		rightEnd = rightEnd - 1;
	}
}

void mergeSort(vector&lt;int&gt; &amp;my_array, vector &lt;int&gt; &amp;tmparray, int left, int right)
{
	if (left &lt; right)
	{
		int center = (left+right) / 2;

		mergeSort(my_array, tmparray, left, center);
		mergeSort(my_array, tmparray, center + 1, right);
		merge(my_array, tmparray, left, center + 1, right);
	}
}
</code></pre>
<p>nur ist die sortierung nun nicht mehr richtig!<br />
bei den zahlen: 54, 43, 23, 87, 34<br />
kommt raus: 43, 54, 23, 43, 54</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2452847</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2452847</guid><dc:creator><![CDATA[Dangling]]></dc:creator><pubDate>Thu, 07 May 2015 11:13:49 GMT</pubDate></item><item><title><![CDATA[Reply to Felder als STL Container on Thu, 07 May 2015 11:18:51 GMT]]></title><description><![CDATA[<p>Dangling schrieb:</p>
<blockquote>
<p>nur ist die sortierung nun nicht mehr richtig!<br />
bei den zahlen: 54, 43, 23, 87, 34<br />
kommt raus: 43, 54, 23, 43, 54</p>
</blockquote>
<p>Tja, dann war das push_back wohl nicht die richtige Lösung. Versuche etwas anderes!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2452849</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2452849</guid><dc:creator><![CDATA[manni66]]></dc:creator><pubDate>Thu, 07 May 2015 11:18:51 GMT</pubDate></item><item><title><![CDATA[Reply to Felder als STL Container on Thu, 07 May 2015 13:17:52 GMT]]></title><description><![CDATA[<p>manni66 schrieb:</p>
<blockquote>
<p>Tja, dann war das push_back wohl nicht die richtige Lösung. Versuche etwas anderes!</p>
</blockquote>
<p>leider kenne ich mich nicht mit stl-containern aus<br />
ich hab nun etwas gegoogelt und es scheint &quot;insert&quot; der richtige befehl zu sein um einen bestimmten wert an eine stelle im vektor einzufuege?!<br />
nur funktioniert es so</p>
<pre><code>tmp_array.insert(tmpPos, my_array[leftPos]);
</code></pre>
<p>leider nicht (als ersatz für</p>
<pre><code>tmp_array[tmpPos] = my_array[leftPos];
</code></pre>
<p>&lt;-hier noch mit arrays)</p>
<p>vllt vorschläge wie ich das richtig ersetzen könnte oder welcher befehl gut wäre?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2452860</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2452860</guid><dc:creator><![CDATA[Dangling]]></dc:creator><pubDate>Thu, 07 May 2015 13:17:52 GMT</pubDate></item><item><title><![CDATA[Reply to Felder als STL Container on Thu, 07 May 2015 13:49:58 GMT]]></title><description><![CDATA[<p>Man kann einen std::vector mit einer definierten Größe leer anlegen bzw. mit einem Funktionsaufruf die gewünschte Größe setzen.<br />
Wie das geht steht in deiner C++-Referenz.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2452865</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2452865</guid><dc:creator><![CDATA[manni66]]></dc:creator><pubDate>Thu, 07 May 2015 13:49:58 GMT</pubDate></item></channel></rss>