<?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[Aufgabe korrekt gelöst ?]]></title><description><![CDATA[<p>Hallo,</p>
<p>meine aufgabe war :</p>
<p>ich soll strings einlesen ,das wort das am meißten vorkommt soll ausgegeben werden und es soll auch angezeigt werden wie oft das wort vorgekommen ist.</p>
<p>leider gibt es keine musterlösung und ich wollte fragen ob es an diesem programm etwas zu bemängeln gibt.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;string&gt;
using namespace std;

int main ()
{
vector&lt;string&gt; svec;
string tmp, word;
int i = 0, biggest = 0, howmuch = 0;
while (cin&gt;&gt;tmp)
{

	svec.push_back(tmp);

	for(vector&lt;string&gt;::iterator iter=svec.begin(); iter!=svec.end(); ++iter )
	{
		for(vector&lt;string&gt;::iterator iter1=svec.begin(); iter1!=svec.end(); ++iter1 )
		{
			if (*iter == *iter1)
			{
				howmuch++;

			}
				if (howmuch &gt; biggest)
				{
					biggest = howmuch;
					word = *iter;
				}
		}
		howmuch = 0;
	}

	cout &lt;&lt; &quot;----------------------&quot;&lt;&lt;endl;
	cout &lt;&lt; &quot;Word: &quot; &lt;&lt; word &lt;&lt;&quot; = &quot; &lt;&lt; biggest &lt;&lt; endl; 
	cout &lt;&lt; &quot;----------------------&quot;&lt;&lt;endl;	
}

}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/290889/aufgabe-korrekt-gelöst</link><generator>RSS for Node</generator><lastBuildDate>Tue, 18 Aug 2026 04:43:12 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/290889.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 05 Aug 2011 14:48:51 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Aufgabe korrekt gelöst ? on Fri, 05 Aug 2011 14:48:51 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>meine aufgabe war :</p>
<p>ich soll strings einlesen ,das wort das am meißten vorkommt soll ausgegeben werden und es soll auch angezeigt werden wie oft das wort vorgekommen ist.</p>
<p>leider gibt es keine musterlösung und ich wollte fragen ob es an diesem programm etwas zu bemängeln gibt.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;string&gt;
using namespace std;

int main ()
{
vector&lt;string&gt; svec;
string tmp, word;
int i = 0, biggest = 0, howmuch = 0;
while (cin&gt;&gt;tmp)
{

	svec.push_back(tmp);

	for(vector&lt;string&gt;::iterator iter=svec.begin(); iter!=svec.end(); ++iter )
	{
		for(vector&lt;string&gt;::iterator iter1=svec.begin(); iter1!=svec.end(); ++iter1 )
		{
			if (*iter == *iter1)
			{
				howmuch++;

			}
				if (howmuch &gt; biggest)
				{
					biggest = howmuch;
					word = *iter;
				}
		}
		howmuch = 0;
	}

	cout &lt;&lt; &quot;----------------------&quot;&lt;&lt;endl;
	cout &lt;&lt; &quot;Word: &quot; &lt;&lt; word &lt;&lt;&quot; = &quot; &lt;&lt; biggest &lt;&lt; endl; 
	cout &lt;&lt; &quot;----------------------&quot;&lt;&lt;endl;	
}

}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2102315</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2102315</guid><dc:creator><![CDATA[kantaki]]></dc:creator><pubDate>Fri, 05 Aug 2011 14:48:51 GMT</pubDate></item><item><title><![CDATA[Reply to Aufgabe korrekt gelöst ? on Fri, 05 Aug 2011 15:43:36 GMT]]></title><description><![CDATA[<p>Hallo kantaki,</p>
<p>kantaki schrieb:</p>
<blockquote>
<p>.. ich wollte fragen ob es an diesem programm etwas zu bemängeln gibt.</p>
</blockquote>
<p>Also nur Kleinigkeiten.<br />
Zunächst der Stil - wenn man korrekt einrückt und einige zu viel gewordenen Leerzeilen enfernt, lässt es sich auch leichter lesen:</p>
<p>kantaki schrieb:</p>
<blockquote>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;string&gt;
using namespace std;

int main ()
{
    vector&lt;string&gt; svec;
    string tmp, word;
    int i = 0, biggest = 0, howmuch = 0;
    while (cin&gt;&gt;tmp)
    {
        svec.push_back(tmp);

        for(vector&lt;string&gt;::iterator iter=svec.begin(); iter!=svec.end(); ++iter )
        {
            for(vector&lt;string&gt;::iterator iter1=svec.begin(); iter1!=svec.end(); ++iter1 )
            {
                if (*iter == *iter1)
                {
                    howmuch++;
                }
                if (howmuch &gt; biggest)
                {
                    biggest = howmuch;
                    word = *iter;
                }
            }
            howmuch = 0;
        }

        cout &lt;&lt; &quot;----------------------&quot;&lt;&lt;endl;
        cout &lt;&lt; &quot;Word: &quot; &lt;&lt; word &lt;&lt;&quot; = &quot; &lt;&lt; biggest &lt;&lt; endl;
        cout &lt;&lt; &quot;----------------------&quot;&lt;&lt;endl;   
    }
}
</code></pre>
</blockquote>
<p>Dann solltest Du Variablen möglichst spät deklarieren. Man würde dann auch eher merken, dass 'int i' gar nicht benötigt wird. Bei dem relativ kleinem Programm fällt das nicht so auf, aber bei größeren Programmen gewinnt man einiges an Übersicht. Kleiner Nebeneffekt wäre, dass das 'howmuch = 0;' oben in Zeile 30 einfach weg fällt.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;string&gt;

int main ()
{
    using namespace std;  // hier reicht das auch hin; so lokal wie möglich
    vector&lt;string&gt; svec;
    for( string tmp; cin&gt;&gt;tmp; ) // 'tmp' wird nur innerhalb der Schleife gebraucht, daher hier for statt while
    {
        svec.push_back(tmp);

        int biggest = 0;
        string word;
        for(vector&lt;string&gt;::iterator iter=svec.begin(); iter!=svec.end(); ++iter )
        {
            int howmuch = 0;
            for(vector&lt;string&gt;::iterator iter1=svec.begin(); iter1!=svec.end(); ++iter1 )
            {
                if (*iter == *iter1)
                {
                    ++howmuch;
                }
                if (howmuch &gt; biggest)
                {
                    biggest = howmuch;
                    word = *iter;
                }
            }
        }

        cout &lt;&lt; &quot;----------------------&quot;&lt;&lt;endl;
        cout &lt;&lt; &quot;Word: &quot; &lt;&lt; word &lt;&lt;&quot; = &quot; &lt;&lt; biggest &lt;&lt; endl;
        cout &lt;&lt; &quot;----------------------&quot;&lt;&lt;endl;   
    }
}
</code></pre>
<p>Eventuell könnte man den Algorithmus zwischen Zeile 13 und 30 noch in eine eigenen Funktion fassen, um es mehr zu strukturieren.<br />
Weiter könnte man noch den Einsatz von Algorithmen aus dem C++-Standard empfehlen, wie z.B. <a href="http://www.cplusplus.com/reference/algorithm/count/" rel="nofollow">count</a>.<br />
Und last not least ließe sich der Algorithmus noch verbessern, wenn die Liste der Worte sortiert wäre .. denk mal drüber nach.</p>
<p>Gruß<br />
Werner</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2102340</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2102340</guid><dc:creator><![CDATA[Werner Salomon]]></dc:creator><pubDate>Fri, 05 Aug 2011 15:43:36 GMT</pubDate></item><item><title><![CDATA[Reply to Aufgabe korrekt gelöst ? on Fri, 05 Aug 2011 16:10:44 GMT]]></title><description><![CDATA[<p>super vielen dank <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>
]]></description><link>https://www.c-plusplus.net/forum/post/2102348</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2102348</guid><dc:creator><![CDATA[kantaki]]></dc:creator><pubDate>Fri, 05 Aug 2011 16:10:44 GMT</pubDate></item><item><title><![CDATA[Reply to Aufgabe korrekt gelöst ? on Fri, 05 Aug 2011 16:16:02 GMT]]></title><description><![CDATA[<p>endl ist schlecht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2102350</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2102350</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Fri, 05 Aug 2011 16:16:02 GMT</pubDate></item><item><title><![CDATA[Reply to Aufgabe korrekt gelöst ? on Fri, 05 Aug 2011 16:22:18 GMT]]></title><description><![CDATA[<p>volkard schrieb:</p>
<blockquote>
<p>endl ist schlecht.</p>
</blockquote>
<p>endl ist gut.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2102351</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2102351</guid><dc:creator><![CDATA[314159265358979]]></dc:creator><pubDate>Fri, 05 Aug 2011 16:22:18 GMT</pubDate></item><item><title><![CDATA[Reply to Aufgabe korrekt gelöst ? on Fri, 05 Aug 2011 16:30:48 GMT]]></title><description><![CDATA[<p>Ich denke, hier muß man Struppi folgen und den Trick mit der map einfach zeigen.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;map&gt;
#include &lt;string&gt;
using namespace std;

int main ()
{
	map&lt;string,int&gt; smap;
	string tmp,word;
	int biggest=0;
	while (cin&gt;&gt;tmp) {
		int howmuch=++smap[tmp];
		if (howmuch &gt; biggest) {
			biggest = howmuch;
			word = tmp;
		}
		cout &lt;&lt; &quot;----------------------\n&quot;;
		cout &lt;&lt; &quot;Word: &quot; &lt;&lt; word &lt;&lt;&quot; = &quot; &lt;&lt; biggest &lt;&lt; '\n';
		cout &lt;&lt; &quot;----------------------\n&quot;;
	}
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2102355</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2102355</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Fri, 05 Aug 2011 16:30:48 GMT</pubDate></item><item><title><![CDATA[Reply to Aufgabe korrekt gelöst ? on Fri, 05 Aug 2011 16:28:26 GMT]]></title><description><![CDATA[<p>314159265358979 schrieb:</p>
<blockquote>
<p>volkard schrieb:</p>
<blockquote>
<p>endl ist schlecht.</p>
</blockquote>
<p>endl ist gut.</p>
</blockquote>
<p>Weißt Du, was endl macht? Falls ja, wie begründest Du, daß hier endl verwendet werden soll?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2102357</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2102357</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Fri, 05 Aug 2011 16:28:26 GMT</pubDate></item><item><title><![CDATA[Reply to Aufgabe korrekt gelöst ? on Fri, 05 Aug 2011 16:37:49 GMT]]></title><description><![CDATA[<p>314159265358979 schrieb:</p>
<blockquote>
<p>volkard schrieb:</p>
<blockquote>
<p>endl ist schlecht.</p>
</blockquote>
<p>endl ist gut.</p>
</blockquote>
<p>endl ist gut, wenn einen die Geschwindigkeit nicht interessiert.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2102361</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2102361</guid><dc:creator><![CDATA[Wurstinator]]></dc:creator><pubDate>Fri, 05 Aug 2011 16:37:49 GMT</pubDate></item><item><title><![CDATA[Reply to Aufgabe korrekt gelöst ? on Fri, 05 Aug 2011 16:56:46 GMT]]></title><description><![CDATA[<p>Wurstinator schrieb:</p>
<blockquote>
<p>314159265358979 schrieb:</p>
<blockquote>
<p>volkard schrieb:</p>
<blockquote>
<p>endl ist schlecht.</p>
</blockquote>
<p>endl ist gut.</p>
</blockquote>
<p>endl ist gut, wenn einen die Geschwindigkeit nicht interessiert.</p>
</blockquote>
<p>Also soll ich mir erst endl angewöhnen, weil die Konsole so langsam ist, und sobald ich in Dateien schreibe, soll ich es mir wieder abgewöhnen. Und später mal überlege ich immer, ob ich '\n' oder endl schreiben sollte, indem ich abschätze, ob zu befürchten ist, daß es schnell gehen soll.<br />
Nö.<br />
endl ist gut, wenn ich endl machen will. endl ist nicht gut, wenn ich nur '\n' machen will.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2102369</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2102369</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Fri, 05 Aug 2011 16:56:46 GMT</pubDate></item><item><title><![CDATA[Reply to Aufgabe korrekt gelöst ? on Fri, 05 Aug 2011 17:20:02 GMT]]></title><description><![CDATA[<p>Ich fand nur die Aussage: &quot;endl ist schlecht.&quot; als solches einfach schwachsinnig. Ich benutze endl, wenn ich \n meine, auch wenn mir gleich alle sagen werden, wie schlecht das doch ist.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2102386</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2102386</guid><dc:creator><![CDATA[314159265358979]]></dc:creator><pubDate>Fri, 05 Aug 2011 17:20:02 GMT</pubDate></item><item><title><![CDATA[Reply to Aufgabe korrekt gelöst ? on Fri, 05 Aug 2011 17:27:10 GMT]]></title><description><![CDATA[<p>314159265358979 schrieb:</p>
<blockquote>
<p>Ich fand nur die Aussage: &quot;endl ist schlecht.&quot; als solches einfach schwachsinnig.</p>
</blockquote>
<p>Na, wie gut, daß Du gefragt hast. Und jetzt darfst Du Dir endl wieder abgewöhnen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2102388</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2102388</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Fri, 05 Aug 2011 17:27:10 GMT</pubDate></item><item><title><![CDATA[Reply to Aufgabe korrekt gelöst ? on Fri, 05 Aug 2011 17:31:54 GMT]]></title><description><![CDATA[<p>Endl sagt für mich aber mehr aus als \n.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2102390</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2102390</guid><dc:creator><![CDATA[314159265358979]]></dc:creator><pubDate>Fri, 05 Aug 2011 17:31:54 GMT</pubDate></item><item><title><![CDATA[Reply to Aufgabe korrekt gelöst ? on Fri, 05 Aug 2011 17:33:53 GMT]]></title><description><![CDATA[<p>314159265358979 schrieb:</p>
<blockquote>
<p>Endl sagt für mich aber mehr aus als \n.</p>
</blockquote>
<p>Mir auch. Deswegen nehme ich es nicht, wenn ich gar nicht flushen will.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2102391</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2102391</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Fri, 05 Aug 2011 17:33:53 GMT</pubDate></item><item><title><![CDATA[Reply to Aufgabe korrekt gelöst ? on Fri, 05 Aug 2011 17:45:59 GMT]]></title><description><![CDATA[<p>wieviel schneller ist denn /n gegenüber endl ?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2102394</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2102394</guid><dc:creator><![CDATA[kantaki]]></dc:creator><pubDate>Fri, 05 Aug 2011 17:45:59 GMT</pubDate></item><item><title><![CDATA[Reply to Aufgabe korrekt gelöst ? on Fri, 05 Aug 2011 17:49:13 GMT]]></title><description><![CDATA[<p>kantaki schrieb:</p>
<blockquote>
<p>wieviel schneller ist denn /n gegenüber endl ?</p>
</blockquote>
<p>Unendlich.</p>
<p>endl flusht den Stream. Was zB bei einer Datei bedeutet, dass diese erst vollstaendig auf die Festplatte geschrieben werden muss bevor endl fertig ist. Das bedeutet seeken auf der Platte, Schreibkopf positionieren und schreiben. Das kann dauern.</p>
<p>Es hebelt ja eine Menge optimierungen des Systems aus: denn solche festplattenzugriffe sind enorm langsam - deshalb wird das alles gecacht und spaeter geschrieben.</p>
<p>Flush ist extrem teuer. Es macht ja auch so gut wie nie Sinn.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2102396</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2102396</guid><dc:creator><![CDATA[Shade Of Mine]]></dc:creator><pubDate>Fri, 05 Aug 2011 17:49:13 GMT</pubDate></item><item><title><![CDATA[Reply to Aufgabe korrekt gelöst ? on Fri, 05 Aug 2011 17:50:19 GMT]]></title><description><![CDATA[<p>aber flushed cout nicht automatisch ?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2102397</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2102397</guid><dc:creator><![CDATA[kantaki]]></dc:creator><pubDate>Fri, 05 Aug 2011 17:50:19 GMT</pubDate></item><item><title><![CDATA[Reply to Aufgabe korrekt gelöst ? on Fri, 05 Aug 2011 17:52:38 GMT]]></title><description><![CDATA[<p>kantaki schrieb:</p>
<blockquote>
<p>aber flushed cout nicht automatisch ?</p>
</blockquote>
<p>cout geht idR auf die Konsole. Die ist so oder so langsam.<br />
Wo endl teuer wird ist, wenn die Daten auf eine Speichermedium geschrieben werden, oder ins Netzwerk oder so.</p>
<p>Deshalb faellt es nicht auf, weils bei cout egal ist. Nur wenn man dann cout ploetzlich auf die Platte redirectet oder einen anderen Stream verwendet der auf die Platte/Netzwerk schreibt - dann bricht die Performance ploetzlich komplett ein.</p>
<p>Deshalb gleich richtig machen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2102400</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2102400</guid><dc:creator><![CDATA[Shade Of Mine]]></dc:creator><pubDate>Fri, 05 Aug 2011 17:52:38 GMT</pubDate></item><item><title><![CDATA[Reply to Aufgabe korrekt gelöst ? on Fri, 05 Aug 2011 17:54:10 GMT]]></title><description><![CDATA[<p>okay danke</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2102401</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2102401</guid><dc:creator><![CDATA[kantaki]]></dc:creator><pubDate>Fri, 05 Aug 2011 17:54:10 GMT</pubDate></item></channel></rss>