<?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[Wert gegeben -&amp;gt; aus Liste beste Annäherung finden]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe folgendes Problem (für die Auswahl des passenden String: 4:3, 16:9, 5:4 ...:<br />
Gegeben ist ein Double-Wert z.B.:<br />
1,34</p>
<p>und eine Liste von Double-Werte:<br />
{ (double)4/3, (double)5/4, (double)16/9, (double)21/9, ... }</p>
<p>Gesucht ist jetzt der Index des Listenelements, das am nächsten am gegebenen Wert ist.</p>
<p>Bietet mir C oder C++ (Visual Studio) mir dafür eine fertige Methode oder Funktion?</p>
<p>Oder muss ich es selber programmieren?</p>
<p>Das würde ich schon hinkriegen, aber vllt. gibts ja was fertiges.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/254756/wert-gegeben-gt-aus-liste-beste-annäherung-finden</link><generator>RSS for Node</generator><lastBuildDate>Sat, 12 Sep 2026 15:51:36 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/254756.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 20 Nov 2009 16:16:12 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Wert gegeben -&amp;gt; aus Liste beste Annäherung finden on Fri, 20 Nov 2009 16:16:12 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe folgendes Problem (für die Auswahl des passenden String: 4:3, 16:9, 5:4 ...:<br />
Gegeben ist ein Double-Wert z.B.:<br />
1,34</p>
<p>und eine Liste von Double-Werte:<br />
{ (double)4/3, (double)5/4, (double)16/9, (double)21/9, ... }</p>
<p>Gesucht ist jetzt der Index des Listenelements, das am nächsten am gegebenen Wert ist.</p>
<p>Bietet mir C oder C++ (Visual Studio) mir dafür eine fertige Methode oder Funktion?</p>
<p>Oder muss ich es selber programmieren?</p>
<p>Das würde ich schon hinkriegen, aber vllt. gibts ja was fertiges.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1811083</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1811083</guid><dc:creator><![CDATA[dlgig]]></dc:creator><pubDate>Fri, 20 Nov 2009 16:16:12 GMT</pubDate></item><item><title><![CDATA[Reply to Wert gegeben -&amp;gt; aus Liste beste Annäherung finden on Fri, 20 Nov 2009 16:34:15 GMT]]></title><description><![CDATA[<p>Eine Kombination aus min_element und passendem Funktionsobjekt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1811098</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1811098</guid><dc:creator><![CDATA[Nanyuki]]></dc:creator><pubDate>Fri, 20 Nov 2009 16:34:15 GMT</pubDate></item><item><title><![CDATA[Reply to Wert gegeben -&amp;gt; aus Liste beste Annäherung finden on Fri, 20 Nov 2009 17:11:08 GMT]]></title><description><![CDATA[<p>dlgig schrieb:</p>
<blockquote>
<p>Bietet mir C oder C++ (Visual Studio) mir dafür eine fertige Methode oder Funktion?</p>
</blockquote>
<p>Ja - der C++-Standard in Kombination mit <a href="http://www.boost.org/doc/libs/1_41_0/libs/bind/bind.html" rel="nofollow">boost.bind</a> bietet Dir (mehr oder weniger) was fertiges. Das sähe dann so aus:</p>
<pre><code class="language-cpp">#include &lt;algorithm&gt;
#include &lt;functional&gt; // minus
#include &lt;iostream&gt;
#include &lt;cmath&gt; // abs(double)
#include &lt;boost/bind.hpp&gt;

int main()
{
    using namespace std;
    using boost::bind;
    const double liste[] = { 4./3, 5./4, 16./9, 21./9 };
    for( double d; cout &lt;&lt; &quot;&gt; &quot;, cin &gt;&gt; d; )
        cout &lt;&lt; *min_element( liste, liste + sizeof(liste)/sizeof(*liste),
                bind( static_cast&lt; double(*)( double ) &gt;( &amp;std::abs ), bind( minus&lt; double &gt;(), _1, d ) ) 
                &lt; bind( static_cast&lt; double(*)( double ) &gt;( &amp;std::abs ), bind( minus&lt; double &gt;(), _2, d ) ) )
            &lt;&lt; &quot; liegt &quot; &lt;&lt; d &lt;&lt; &quot; am naechsten&quot; &lt;&lt; endl;
    return 0;
}
</code></pre>
<p>Die Ausgabe ist z.B.:</p>
<pre><code>&gt; 1.2
1.25 liegt 1.2 am naechsten
&gt; 1.3
1.33333 liegt 1.3 am naechsten
&gt; 1.8
1.77778 liegt 1.8 am naechsten
&gt; 25
2.33333 liegt 25 am naechsten
&gt;
</code></pre>
<p>Gruß<br />
Werner</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1811120</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1811120</guid><dc:creator><![CDATA[Werner Salomon]]></dc:creator><pubDate>Fri, 20 Nov 2009 17:11:08 GMT</pubDate></item></channel></rss>