<?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[max index berechnen]]></title><description><![CDATA[<p>hallo,</p>
<p>ich versuche max Element und dessen Index berechnen. Meine Programmasschnitt:</p>
<pre><code>#ifndef max
#define max(a,b)   (((a) &gt; (b)) ? (a) : (b))
#endif

double max_element( std::vector&lt;double&gt; vector )
{
	double max_element= 0.0;

		for(int l = 0; l &lt; (int)vector.size(); l++)
		{
			max_dist = max(vector[l], max_dist);
		}

	return max_element;
}
</code></pre>
<p>wie kann ich index des max_element finden?</p>
<p>danke und gruß<br />
ns</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/264353/max-index-berechnen</link><generator>RSS for Node</generator><lastBuildDate>Fri, 04 Sep 2026 18:00:20 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/264353.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 06 Apr 2010 16:05:31 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to max index berechnen on Tue, 06 Apr 2010 16:05:31 GMT]]></title><description><![CDATA[<p>hallo,</p>
<p>ich versuche max Element und dessen Index berechnen. Meine Programmasschnitt:</p>
<pre><code>#ifndef max
#define max(a,b)   (((a) &gt; (b)) ? (a) : (b))
#endif

double max_element( std::vector&lt;double&gt; vector )
{
	double max_element= 0.0;

		for(int l = 0; l &lt; (int)vector.size(); l++)
		{
			max_dist = max(vector[l], max_dist);
		}

	return max_element;
}
</code></pre>
<p>wie kann ich index des max_element finden?</p>
<p>danke und gruß<br />
ns</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1878413</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1878413</guid><dc:creator><![CDATA[ns]]></dc:creator><pubDate>Tue, 06 Apr 2010 16:05:31 GMT</pubDate></item><item><title><![CDATA[Reply to max index berechnen on Tue, 06 Apr 2010 16:11:34 GMT]]></title><description><![CDATA[<p>Mit einem Tick besseren Deutsch, würde ich wohl verstehen, was genau du möchtest. Außerdem ist dein Code fehlerhaft und das nicht von der Logik her.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1878419</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1878419</guid><dc:creator><![CDATA[freaky]]></dc:creator><pubDate>Tue, 06 Apr 2010 16:11:34 GMT</pubDate></item><item><title><![CDATA[Reply to max index berechnen on Tue, 06 Apr 2010 16:16:06 GMT]]></title><description><![CDATA[<p>Es ist doch immer wieder schön zu sehen, dass die STL ein wenig mitmacht. <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><a href="http://www.cplusplus.com/reference/algorithm/max_element/" rel="nofollow">std::max_element</a></p>
<p>Da siehst du btw. auch, wie man das korrekt implementiert.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1878423</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1878423</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Tue, 06 Apr 2010 16:16:06 GMT</pubDate></item><item><title><![CDATA[Reply to max index berechnen on Tue, 06 Apr 2010 16:18:44 GMT]]></title><description><![CDATA[<p>drakon schrieb:</p>
<blockquote>
<p>...</p>
</blockquote>
<p>Gib zu, du hast geraten was er wollte <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1878424</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1878424</guid><dc:creator><![CDATA[freaky]]></dc:creator><pubDate>Tue, 06 Apr 2010 16:18:44 GMT</pubDate></item><item><title><![CDATA[Reply to max index berechnen on Tue, 06 Apr 2010 16:19:18 GMT]]></title><description><![CDATA[<p>Ersetz das Makro durch eine inline-Funktion. <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/1878426</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1878426</guid><dc:creator><![CDATA[Janjan]]></dc:creator><pubDate>Tue, 06 Apr 2010 16:19:18 GMT</pubDate></item><item><title><![CDATA[Reply to max index berechnen on Tue, 06 Apr 2010 16:27:34 GMT]]></title><description><![CDATA[<p>Unter der Annahme, dass du dies zur Übung selber machst, empfehle ich dir mal nicht den <a href="http://www.cplusplus.com/reference/algorithm/max_element/" rel="nofollow">fertigen Algorithmus aus der STL</a> dazu, sondern erweitere deinen Ansatz. Du nimmst am besten noch eine zweite Variable in der du den Index speicherst. Und jedes mal, wenn du ein neues größtes Element gefunden hast, änderst du diese Variable. Dann brauchst du auch nicht dieses doofe Makro (wie kommst du da eigentlich drauf?), abgesehen davon gibt es natürlich auch ein <a href="http://www.cplusplus.com/reference/algorithm/max/" rel="nofollow">typensicheres max</a>, ebenfalls aus der STL.</p>
<pre><code class="language-cpp">std::pair&lt;int,double&gt; max_element( std::vector&lt;double&gt; vector )   // Wir wollen zwei Werte zurückgeben
{
    double max_elem = -1/0.; // (-Unendlich) dürfte die kleinste Zahl sein :-)
    int max_index = 0;

        for(unsigned int i = 0; i &lt; vector.size(); i++) // Was spricht gegen unsigned int? int musstest du aus gutem Grund von Hand casten, denn es gibt keine negativen Indizes
        {
            if (vector[i] &gt; max_elem)  // Momentanes Element ist größer als bisheriges Maximum
                  {
                        max_elem = vector[i];   // Speichere neues Maximum...
                        max_index = i;          // ...und passenden Index
                  }
        }

    return std::pair&lt;int, double&gt;(max_index,max_elem);
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1878430</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1878430</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Tue, 06 Apr 2010 16:27:34 GMT</pubDate></item><item><title><![CDATA[Reply to max index berechnen on Wed, 07 Apr 2010 11:56:27 GMT]]></title><description><![CDATA[<p>-1/0.; ist eine dubiose Operation, da die interne Darstellung von float/double-Typen implementationsabhängig ist. Besser ist es</p>
<pre><code class="language-cpp">numeric_limits&lt;double&gt;::min()
</code></pre>
<p>zu verwenden, oder noch besser, weil einfacher,</p>
<pre><code class="language-cpp">max_elem = vector[0];
</code></pre>
<p>zu setzen. Die Schleife kann dann ab 1 laufen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1878771</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1878771</guid><dc:creator><![CDATA[make it right]]></dc:creator><pubDate>Wed, 07 Apr 2010 11:56:27 GMT</pubDate></item><item><title><![CDATA[Reply to max index berechnen on Wed, 07 Apr 2010 12:08:41 GMT]]></title><description><![CDATA[<p>make it right schrieb:</p>
<blockquote>
<p>-1/0.; ist eine dubiose Operation, da die interne Darstellung von float/double-Typen implementationsabhängig ist. Besser ist es</p>
<pre><code class="language-cpp">numeric_limits&lt;double&gt;::min()
</code></pre>
<p>zu verwenden, oder noch besser, weil einfacher,</p>
<pre><code class="language-cpp">max_elem = vector[0];
</code></pre>
<p>zu setzen. Die Schleife kann dann ab 1 laufen.</p>
</blockquote>
<p>Auch das ist schlecht. Denn was ist wenn die Liste leer ist?</p>
<p>Index auf -1 setzen und zur Not dies auch zurückgeben, was dem geneigten Nutzer vermittelt das kein maximales gefunden wurde (weil leer).</p>
<p>EDIT:<br />
Und warum eigentlich eine Kopie von dem vector übergeben statt einer Referenz?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1878782</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1878782</guid><dc:creator><![CDATA[Fellhuhn]]></dc:creator><pubDate>Wed, 07 Apr 2010 12:08:41 GMT</pubDate></item><item><title><![CDATA[Reply to max index berechnen on Wed, 07 Apr 2010 13:21:48 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/17700">@Fellhuhn</a> &amp; make it right: Weil der Fragesteller offensichtlich ein absoluter Beginner ist. Ich wollte ihn jetzt nicht noch mit limits, Referenzen und Sonderfällen verwirren sondern ihm erstmal nur zeigen wie man sowas überhaupt macht. Das Programm lässt er dann dreimal laufen und freut sich dass es funktioniert. Und er versteht hoffentlich die Grundidee. Das dabei vom Expertenstandpunkt gesehen nicht alles optimal gelöst ist, ist dabei doch völlig egal - wahrscheinlich schreibt er schon jetzt das nächste Übungsprogramm und hat dieses längst vergessen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1878839</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1878839</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Wed, 07 Apr 2010 13:21:48 GMT</pubDate></item><item><title><![CDATA[Reply to max index berechnen on Wed, 07 Apr 2010 13:42:05 GMT]]></title><description><![CDATA[<p>Fellhuhn schrieb:</p>
<blockquote>
<p>Auch das ist schlecht. Denn was ist wenn die Liste leer ist?</p>
</blockquote>
<p>das musst du sowieso abfangen, sonst gibts du ja den index 0 zurück.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1878861</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1878861</guid><dc:creator><![CDATA[Shade Of Mine]]></dc:creator><pubDate>Wed, 07 Apr 2010 13:42:05 GMT</pubDate></item><item><title><![CDATA[Reply to max index berechnen on Wed, 07 Apr 2010 14:51:27 GMT]]></title><description><![CDATA[<p>So, hier mal die funktionale Variante fuer C++. Der Typparameter double kann auch noch generalisiert werden:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;utility&gt;
#include &lt;numeric&gt;

struct Triple
{
    int currentIndex_;
    int maxIndex_;
    double value_;
    Triple(int currentIndex, int maxIndex, double value) :
        currentIndex_(currentIndex), maxIndex_(maxIndex), value_(value) {}
};

// define a reduce function for accumulate (fold)
Triple maxIndex(Triple acc, double value)
{
    if (acc.value_ &lt; value)
    {
        acc.maxIndex_ = acc.currentIndex_;
        ++acc.currentIndex_;
        acc.value_ = value;
    }
    else
    {
        ++acc.currentIndex_;
    }
    return acc;
}

int main()
{
    double i[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9, 8, 7, 6 };
    Triple v = std::accumulate(i+1,i+14, Triple(1,0,i[0]), maxIndex);
    std::cout &lt;&lt; &quot;Index: &quot; &lt;&lt; v.maxIndex_ &lt;&lt; &quot; Value: &quot; &lt;&lt; v.value_ &lt;&lt; std::endl;

    return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1878892</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1878892</guid><dc:creator><![CDATA[knivil]]></dc:creator><pubDate>Wed, 07 Apr 2010 14:51:27 GMT</pubDate></item><item><title><![CDATA[Reply to max index berechnen on Wed, 07 Apr 2010 14:27:49 GMT]]></title><description><![CDATA[<p>knivil schrieb:</p>
<blockquote>
<p>So, hier mal die funktionale Variante fuer C++. Der Typparameter double kann auch noch generalisiert werden:</p>
</blockquote>
<p>wtf?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1878898</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1878898</guid><dc:creator><![CDATA[Shade Of Mine]]></dc:creator><pubDate>Wed, 07 Apr 2010 14:27:49 GMT</pubDate></item><item><title><![CDATA[Reply to max index berechnen on Wed, 07 Apr 2010 14:57:27 GMT]]></title><description><![CDATA[<p>Shade Of Mine schrieb:</p>
<blockquote>
<p>wtf?</p>
</blockquote>
<p>Naja:<br />
1te Ueberlegung: Wie wuerde ich das funktional machen -&gt; fold ...<br />
2te Ueberlegung: Was ist in C++ aequivalent zu fold -&gt; accumulate ...</p>
<p>accumulate ist eine Funktion hoeherer Ordnung (hat als Parameter eine Funktion) ... deswegen funktional ...</p>
<p>Wollte man nur das maximale Element wissen, so reduziert sich die Vergleichsfunktion auf eben std::max.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1878904</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1878904</guid><dc:creator><![CDATA[knivil]]></dc:creator><pubDate>Wed, 07 Apr 2010 14:57:27 GMT</pubDate></item><item><title><![CDATA[Reply to max index berechnen on Wed, 07 Apr 2010 14:49:15 GMT]]></title><description><![CDATA[<p>da hat mir das kurze fold aber besser gefallen:</p>
<pre><code class="language-cpp">Triple maxIndex(Triple acc, double value) 
{
  return Triple(
    acc.currentIndex,
    acc.value &lt; value ? acc.maxIndex : acc.currentIndex,
    acc.value &lt; value ? acc.value : value
  );
}
</code></pre>
<p>cooler ist natürlich wenn Triple nur einen int als currentIndex hat und ein Objekt für das index/max pair, dann kannst du</p>
<pre><code class="language-cpp">Triple maxIndex(Triple acc, double value) 
{
  return Triple(
    acc.currentIndex+1,
    acc.value &lt; value ? 
      acc.pair : Pair(value, acc.currentIndex)
  );
}
</code></pre>
<p>machen</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1878908</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1878908</guid><dc:creator><![CDATA[Shade Of Mine]]></dc:creator><pubDate>Wed, 07 Apr 2010 14:49:15 GMT</pubDate></item><item><title><![CDATA[Reply to max index berechnen on Wed, 07 Apr 2010 14:54:40 GMT]]></title><description><![CDATA[<p>Ja, die Loesung war jetzt auch nur schnell out of the box um das Prinzip zu verdeutlichen. Sicher gibt es noch einige Kniffe, komplett getestet ist es auch nicht ... Deine Variante wuerde ich allein der Laenge wegen vorziehen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1878909</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1878909</guid><dc:creator><![CDATA[knivil]]></dc:creator><pubDate>Wed, 07 Apr 2010 14:54:40 GMT</pubDate></item><item><title><![CDATA[Reply to max index berechnen on Thu, 08 Apr 2010 06:56:03 GMT]]></title><description><![CDATA[<p>Da gefällt mir die Lösung von SeppJ 100x besser.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1879134</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1879134</guid><dc:creator><![CDATA[Fellhuhn]]></dc:creator><pubDate>Thu, 08 Apr 2010 06:56:03 GMT</pubDate></item></channel></rss>