<?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[STL: Assoziative Container und find]]></title><description><![CDATA[<p>Hallo!</p>
<p>Folgendes Problem: Ich habe mit einer Map eine einfache Container-Kette erstellt, die einen Key (Ort) und seine PLZ in einem pair-Struct in einer Map speichert. Jetzt soll der Benutzer einen Ort eingeben können und die PLZ ausgegeben haben. Doch als Ausgabe erfolgt immer &quot;Nicht gefunden!&quot;, wenn ich jedoch den C-String durch ein handgeschriebenen Ort im Code ersetze, ist die PLZ vorhanden.<br />
Der Code:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;utility&gt;
#include &lt;map&gt;

using namespace std;

int main() {

    char ort[10];

    typedef map&lt;char*, int&gt; plz_map;
    plz_map plz;

    plz.insert(pair&lt;char*, int&gt;(&quot;Hannover&quot;, 12345));
    plz[&quot;Muenchen&quot;] = 54321;
    plz[&quot;Lehnin&quot;] = 14797;
    cout &lt;&lt; &quot;PLZ suchen\nBitte Ort eingeben: &quot;;
    cin.getline(ort, sizeof(ort));
    map&lt;char*, int&gt;::iterator fund = plz.find(ort);

    if(fund != plz.end()) { // liefert &quot;Nicht gefunden!&quot;
        cout &lt;&lt; ort &lt;&lt; &quot; wurde gefunden. PLZ: &quot; &lt;&lt; plz[ort] &lt;&lt; endl;
    }
    else {
        cout &lt;&lt; &quot;Nicht gefunden!&quot; &lt;&lt; endl;
    }

    plz_map::iterator iter = plz.begin();

    while(iter != plz.end()) {  // zeigt alle Inhalte
        cout &lt;&lt; iter-&gt;first &lt;&lt; &quot; : &quot; &lt;&lt; iter-&gt;second &lt;&lt; endl;
        iter++;
    }

    cout &lt;&lt; plz.count(&quot;Lehnin&quot;) &lt;&lt; endl; // 14797 
    cout &lt;&lt; plz.count(ort) &lt;&lt; endl; // liefert 0

    system(&quot;pause&quot;);
    return 0;
}
</code></pre>
<p>Bin recht ratlos, hat jemand eine Idee, warum ein var = &quot;Lehnin&quot;, count(var) eine 0 liefert, während count(&quot;Lehnin&quot;) den Wert findet?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/237992/stl-assoziative-container-und-find</link><generator>RSS for Node</generator><lastBuildDate>Wed, 23 Sep 2026 05:01:17 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/237992.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 04 Apr 2009 18:08:14 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to STL: Assoziative Container und find on Sat, 04 Apr 2009 18:08:14 GMT]]></title><description><![CDATA[<p>Hallo!</p>
<p>Folgendes Problem: Ich habe mit einer Map eine einfache Container-Kette erstellt, die einen Key (Ort) und seine PLZ in einem pair-Struct in einer Map speichert. Jetzt soll der Benutzer einen Ort eingeben können und die PLZ ausgegeben haben. Doch als Ausgabe erfolgt immer &quot;Nicht gefunden!&quot;, wenn ich jedoch den C-String durch ein handgeschriebenen Ort im Code ersetze, ist die PLZ vorhanden.<br />
Der Code:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;utility&gt;
#include &lt;map&gt;

using namespace std;

int main() {

    char ort[10];

    typedef map&lt;char*, int&gt; plz_map;
    plz_map plz;

    plz.insert(pair&lt;char*, int&gt;(&quot;Hannover&quot;, 12345));
    plz[&quot;Muenchen&quot;] = 54321;
    plz[&quot;Lehnin&quot;] = 14797;
    cout &lt;&lt; &quot;PLZ suchen\nBitte Ort eingeben: &quot;;
    cin.getline(ort, sizeof(ort));
    map&lt;char*, int&gt;::iterator fund = plz.find(ort);

    if(fund != plz.end()) { // liefert &quot;Nicht gefunden!&quot;
        cout &lt;&lt; ort &lt;&lt; &quot; wurde gefunden. PLZ: &quot; &lt;&lt; plz[ort] &lt;&lt; endl;
    }
    else {
        cout &lt;&lt; &quot;Nicht gefunden!&quot; &lt;&lt; endl;
    }

    plz_map::iterator iter = plz.begin();

    while(iter != plz.end()) {  // zeigt alle Inhalte
        cout &lt;&lt; iter-&gt;first &lt;&lt; &quot; : &quot; &lt;&lt; iter-&gt;second &lt;&lt; endl;
        iter++;
    }

    cout &lt;&lt; plz.count(&quot;Lehnin&quot;) &lt;&lt; endl; // 14797 
    cout &lt;&lt; plz.count(ort) &lt;&lt; endl; // liefert 0

    system(&quot;pause&quot;);
    return 0;
}
</code></pre>
<p>Bin recht ratlos, hat jemand eine Idee, warum ein var = &quot;Lehnin&quot;, count(var) eine 0 liefert, während count(&quot;Lehnin&quot;) den Wert findet?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1690976</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1690976</guid><dc:creator><![CDATA[Ad aCTa]]></dc:creator><pubDate>Sat, 04 Apr 2009 18:08:14 GMT</pubDate></item><item><title><![CDATA[Reply to STL: Assoziative Container und find on Sat, 04 Apr 2009 18:11:19 GMT]]></title><description><![CDATA[<p>Nimm std::string statt char*. Du kannst zwei char-arrays nicht mit = vergleichen. Und genau das macht std::map intern.</p>
<p>(std::string ist sogut wie immer gegenüber char* im Vorteil...)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1690977</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1690977</guid><dc:creator><![CDATA[The-Kenny]]></dc:creator><pubDate>Sat, 04 Apr 2009 18:11:19 GMT</pubDate></item><item><title><![CDATA[Reply to STL: Assoziative Container und find on Sat, 04 Apr 2009 18:22:32 GMT]]></title><description><![CDATA[<p>The-Kenny schrieb:</p>
<blockquote>
<p>Nimm std::string statt char*. Du kannst zwei char-arrays nicht mit = vergleichen. Und genau das macht std::map intern.</p>
<p>(std::string ist sogut wie immer gegenüber char* im Vorteil...)</p>
</blockquote>
<p>Quatsch... std::map verwendet eine Vergleichsfunktionalität die über die Templateargument festgelegt werden kann. Da lässt sich auch was für const char* finden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1690984</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1690984</guid><dc:creator><![CDATA[David_pb]]></dc:creator><pubDate>Sat, 04 Apr 2009 18:22:32 GMT</pubDate></item><item><title><![CDATA[Reply to STL: Assoziative Container und find on Sat, 04 Apr 2009 18:23:26 GMT]]></title><description><![CDATA[<p>Danke, genau das wars. Noch ein Fehler in meinem Buch, grrr <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f621.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--pouting_face"
      title=":rage:"
      alt="😡"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1690985</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1690985</guid><dc:creator><![CDATA[Ad aCTa]]></dc:creator><pubDate>Sat, 04 Apr 2009 18:23:26 GMT</pubDate></item><item><title><![CDATA[Reply to STL: Assoziative Container und find on Sat, 04 Apr 2009 18:38:11 GMT]]></title><description><![CDATA[<p>David_pb schrieb:</p>
<blockquote>
<p>The-Kenny schrieb:</p>
<blockquote>
<p>Nimm std::string statt char*. Du kannst zwei char-arrays nicht mit = vergleichen. Und genau das macht std::map intern.</p>
<p>(std::string ist sogut wie immer gegenüber char* im Vorteil...)</p>
</blockquote>
<p>Quatsch... std::map verwendet eine Vergleichsfunktionalität die über die Templateargument festgelegt werden kann. Da lässt sich auch was für const char* finden.</p>
</blockquote>
<p>Ist mir bewusst, aber ich bin Faul und hatte nicht wirklich Lust viel zu tippen. Also habe ich es mir frei genommen, dies wegzulassen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1690989</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1690989</guid><dc:creator><![CDATA[The-Kenny]]></dc:creator><pubDate>Sat, 04 Apr 2009 18:38:11 GMT</pubDate></item><item><title><![CDATA[Reply to STL: Assoziative Container und find on Sat, 04 Apr 2009 20:07:28 GMT]]></title><description><![CDATA[<p>The-Kenny schrieb:</p>
<blockquote>
<p>David_pb schrieb:</p>
<blockquote>
<p>The-Kenny schrieb:</p>
<blockquote>
<p>Nimm std::string statt char*. Du kannst zwei char-arrays nicht mit = vergleichen. Und genau das macht std::map intern.</p>
<p>(std::string ist sogut wie immer gegenüber char* im Vorteil...)</p>
</blockquote>
<p>Quatsch... std::map verwendet eine Vergleichsfunktionalität die über die Templateargument festgelegt werden kann. Da lässt sich auch was für const char* finden.</p>
</blockquote>
<p>Ist mir bewusst, aber ich bin Faul und hatte nicht wirklich Lust viel zu tippen. Also habe ich es mir frei genommen, dies wegzulassen.</p>
</blockquote>
<p>Und stattdessem was komplett unsinniges zu schreiben?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1691020</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1691020</guid><dc:creator><![CDATA[David_pb]]></dc:creator><pubDate>Sat, 04 Apr 2009 20:07:28 GMT</pubDate></item><item><title><![CDATA[Reply to STL: Assoziative Container und find on Sat, 04 Apr 2009 20:44:40 GMT]]></title><description><![CDATA[<p>David_pb schrieb:</p>
<blockquote>
<p>Und stattdessem was komplett unsinniges zu schreiben?</p>
</blockquote>
<p>Meine Güte. &quot;komplett unsinnig&quot; war das ja wohl kaum, nur nicht vollständig. Standardmäßig wird = verwendet und std::strings zu verwenden ist normalerweise auch keine schlechte Idee.</p>
<p>Also lass bitte das Herumgestänkere, danke.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1691033</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1691033</guid><dc:creator><![CDATA[nman]]></dc:creator><pubDate>Sat, 04 Apr 2009 20:44:40 GMT</pubDate></item><item><title><![CDATA[Reply to STL: Assoziative Container und find on Sat, 04 Apr 2009 21:03:40 GMT]]></title><description><![CDATA[<p>Also gibt es auch keine Lösung, das mit C-Strings zu realisieren (nur so aus Interesse), oder kann man das mit 'nem Funktor und strcmp machen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1691038</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1691038</guid><dc:creator><![CDATA[Ad aCTa]]></dc:creator><pubDate>Sat, 04 Apr 2009 21:03:40 GMT</pubDate></item><item><title><![CDATA[Reply to STL: Assoziative Container und find on Sat, 04 Apr 2009 21:40:10 GMT]]></title><description><![CDATA[<p>Ad aCTa schrieb:</p>
<blockquote>
<p>Also gibt es auch keine Lösung, das mit C-Strings zu realisieren (nur so aus Interesse), oder kann man das mit 'nem Funktor und strcmp machen?</p>
</blockquote>
<p>Letzteres ist kein Problem, wird sogar in der <a href="http://www.sgi.com/tech/stl/Map.html" rel="nofollow">SGI-Doku zu map</a> vorgezeigt. Aber std::string schadet normalerweise nicht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1691042</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1691042</guid><dc:creator><![CDATA[nman]]></dc:creator><pubDate>Sat, 04 Apr 2009 21:40:10 GMT</pubDate></item><item><title><![CDATA[Reply to STL: Assoziative Container und find on Sun, 05 Apr 2009 07:19:25 GMT]]></title><description><![CDATA[<p>nman schrieb:</p>
<blockquote>
<p>David_pb schrieb:</p>
<blockquote>
<p>Und stattdessem was komplett unsinniges zu schreiben?</p>
</blockquote>
<p>Meine Güte. &quot;komplett unsinnig&quot; war das ja wohl kaum, nur nicht vollständig. Standardmäßig wird = verwendet und std::strings zu verwenden ist normalerweise auch keine schlechte Idee.</p>
<p>Also lass bitte das Herumgestänkere, danke.</p>
</blockquote>
<p>Tut mir leid das ich &quot;Herumstänkere&quot;. Der Tipp std:string zu verwenden war natürlich durchaus gut! Aber der Rest des Posts war nicht unvollständig sondern falsch. Standardmäßig wird im std::map nicht = sondern <em>less</em> verwendet. Klar läuft das im Endeffekt auf die <em>Funktionalität</em> von &quot;=&quot; raus, aber gerade Anfänger wollen sowas irgendwie gern falsch verstehen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1691092</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1691092</guid><dc:creator><![CDATA[David_pb]]></dc:creator><pubDate>Sun, 05 Apr 2009 07:19:25 GMT</pubDate></item><item><title><![CDATA[Reply to STL: Assoziative Container und find on Sun, 05 Apr 2009 08:45:49 GMT]]></title><description><![CDATA[<p>Okay, habe es mit einem eigenen Funktor mit C-Strings gelößt:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;utility&gt;
#include &lt;map&gt;

using namespace std;

struct cmp {
    bool operator() (const char* s1, const char* s2) const {
        return strcmp(s1, s2) &lt; 0;
    }
};

int main() {

    char ort[10];

    typedef map&lt;char*, int, cmp&gt; plz_map;
    plz_map plz;

    plz.insert(pair&lt;char*, int&gt;(&quot;Hannover&quot;, 12345));
    plz[&quot;Muenchen&quot;] = 54321;
    plz[&quot;Lehnin&quot;] = 14797;
    cout &lt;&lt; &quot;PLZ suchen\nBitte Ort eingeben: &quot;;
    cin &gt;&gt; ort;
    map&lt;char*, int, cmp&gt;::iterator fund = plz.find(ort);

    if(fund != plz.end()) {
        cout &lt;&lt; ort &lt;&lt; &quot; wurde gefunden. PLZ: &quot; &lt;&lt; plz[ort] &lt;&lt; endl;
    }
    else {
        cout &lt;&lt; &quot;Nicht gefunden!&quot; &lt;&lt; endl;
    }

    plz_map::iterator iter = plz.begin();

    while(iter != plz.end()) {  // zeigt alle Inhalte
        cout &lt;&lt; iter-&gt;first &lt;&lt; &quot; : &quot; &lt;&lt; iter-&gt;second &lt;&lt; endl;
        iter++;
    }

    cout &lt;&lt; plz.count(&quot;Lehnin&quot;) &lt;&lt; endl; // 14797
    cout &lt;&lt; plz.count(ort) &lt;&lt; endl; // liefert 1

    system(&quot;pause&quot;);
    return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1691101</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1691101</guid><dc:creator><![CDATA[Ad aCTa]]></dc:creator><pubDate>Sun, 05 Apr 2009 08:45:49 GMT</pubDate></item></channel></rss>