<?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[std::map mit eigenem Ordnungsvergleich]]></title><description><![CDATA[<p>Wenn ich als Key-Typ bei der Map was nehme, was sich nicht mit '&lt;' vergleichen lässt, muss ich ja eine Klasse angeben, mit der er vergleicht, richtig? Ich dachte jetzt, das geht dann so, aber anscheinend nicht... <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /> Sieht vielleicht jemand den Fehler?</p>
<pre><code class="language-cpp">#include &lt;map&gt;

using std::map;
using std::binary_function;

class Item {
	int value;
  public:
	Item(int value) {
		this-&gt;value = value;
	}
	Item(Item&amp; obj) {
		value = obj.getValue();
	}
	int getValue() {
		return value;
	}
};

class Less : public binary_function&lt;Item*, Item*, bool&gt; {
  public:
	Less() {}
	bool operator()(const Item*&amp; a, const Item*&amp; b) {
		return const_cast&lt;Item*&gt;(a)-&gt;getValue() &lt; const_cast&lt;Item*&gt;(b)-&gt;getValue();
	}
};

int main() {
	map&lt;Item*, int, Less&gt; foo;
	foo[new Item(5)] = 5;
	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/158094/std-map-mit-eigenem-ordnungsvergleich</link><generator>RSS for Node</generator><lastBuildDate>Wed, 09 Sep 2026 13:38:20 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/158094.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 01 Sep 2006 11:37:53 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to std::map mit eigenem Ordnungsvergleich on Fri, 01 Sep 2006 11:37:53 GMT]]></title><description><![CDATA[<p>Wenn ich als Key-Typ bei der Map was nehme, was sich nicht mit '&lt;' vergleichen lässt, muss ich ja eine Klasse angeben, mit der er vergleicht, richtig? Ich dachte jetzt, das geht dann so, aber anscheinend nicht... <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /> Sieht vielleicht jemand den Fehler?</p>
<pre><code class="language-cpp">#include &lt;map&gt;

using std::map;
using std::binary_function;

class Item {
	int value;
  public:
	Item(int value) {
		this-&gt;value = value;
	}
	Item(Item&amp; obj) {
		value = obj.getValue();
	}
	int getValue() {
		return value;
	}
};

class Less : public binary_function&lt;Item*, Item*, bool&gt; {
  public:
	Less() {}
	bool operator()(const Item*&amp; a, const Item*&amp; b) {
		return const_cast&lt;Item*&gt;(a)-&gt;getValue() &lt; const_cast&lt;Item*&gt;(b)-&gt;getValue();
	}
};

int main() {
	map&lt;Item*, int, Less&gt; foo;
	foo[new Item(5)] = 5;
	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1128983</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1128983</guid><dc:creator><![CDATA[DollyDuplex]]></dc:creator><pubDate>Fri, 01 Sep 2006 11:37:53 GMT</pubDate></item><item><title><![CDATA[Reply to std::map mit eigenem Ordnungsvergleich on Fri, 01 Sep 2006 11:54:33 GMT]]></title><description><![CDATA[<p>DollyDuplex schrieb:</p>
<blockquote>
<p>Ich dachte jetzt, das geht dann so, aber anscheinend nicht...</p>
</blockquote>
<p>Ich hoffe es antwortet keiner mehr auf solche Fehlerbeschreibungen</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1128998</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1128998</guid><dc:creator><![CDATA[templäd]]></dc:creator><pubDate>Fri, 01 Sep 2006 11:54:33 GMT</pubDate></item><item><title><![CDATA[Reply to std::map mit eigenem Ordnungsvergleich on Fri, 01 Sep 2006 11:56:11 GMT]]></title><description><![CDATA[<p>DollyDuplex schrieb:</p>
<blockquote>
<p>Sieht vielleicht jemand den Fehler?</p>
</blockquote>
<p>die kristallkugel haben wir versetzt.<br />
<a href="http://www.lugbz.org/documents/smart-questions_de.html" rel="nofollow">Wie man Fragen richtig stellt</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1129000</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1129000</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Fri, 01 Sep 2006 11:56:11 GMT</pubDate></item><item><title><![CDATA[Reply to std::map mit eigenem Ordnungsvergleich on Fri, 01 Sep 2006 11:57:34 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">foo[new Item(5)] = 5;
</code></pre>
<p><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=":confused:"
      alt="😕"
    /> <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=":confused:"
      alt="😕"
    /> <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=":confused:"
      alt="😕"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1129001</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1129001</guid><dc:creator><![CDATA[THX 1138]]></dc:creator><pubDate>Fri, 01 Sep 2006 11:57:34 GMT</pubDate></item><item><title><![CDATA[Reply to std::map mit eigenem Ordnungsvergleich on Fri, 01 Sep 2006 11:59:11 GMT]]></title><description><![CDATA[<p>DollyDuplex schrieb:</p>
<blockquote>
<p>Sieht vielleicht jemand den Fehler?</p>
</blockquote>
<p>Ja. Dennoch wäre's nicht ungeschickt gewesen zumindest anzudeuten was denn &quot;dann nicht so geht&quot;.</p>
<pre><code class="language-cpp">class Item {
public:
    Item( int value ) : value(value) { } // kommt aufs selbe raus
    Item( Item&amp; obj ) : value(obj.value) { } // class 'Item' is implicitly friends with itself
    int getValue() const { return value; } // const-correctness

private:
  int value;
};

// der Funktor muss nicht von binary_function abgeleitet sein, sollte aber wenn möglich halbwegs
// sinnvoll benannt sein.
struct ItemValueLess
{
  //Less() {} // Konstruktor unnoetig
  // hier war dein Fehler: der Pointer muss const sein, nicht unbedingt das Item worauf er zeigt;
  // und &quot;foo*&amp;&quot; ist nicht unbedingt billiger als schlicht zu kopieren.
  bool operator()( Item const* const a, Item const* const b) const
  {
    return a-&gt;getValue() &lt; b-&gt;getValue(); // weg mit dem cast
  }
};

int main() {
    map&lt;Item*, int, ItemValueLess&gt; foo;
    foo[new Item(5)] = 5; // wer raeumt das bloss wieder auf? warum nimmst du keine Objekte?
    return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1129003</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1129003</guid><dc:creator><![CDATA[finix]]></dc:creator><pubDate>Fri, 01 Sep 2006 11:59:11 GMT</pubDate></item><item><title><![CDATA[Reply to std::map mit eigenem Ordnungsvergleich on Fri, 01 Sep 2006 12:13:20 GMT]]></title><description><![CDATA[<p>ich denke irgendwie an</p>
<pre><code class="language-cpp">#include &lt;map&gt;

using std::map;

class Item {
    int value;
  public:
    Item(int value) {
        this-&gt;value = value;
    }
    Item(Item&amp; obj) {
        value = obj.getValue();
    }
    int getValue() {
        return value;
    }
    friend bool operator&lt;(Item const&amp; a,Item const&amp; b){
        return a.value&lt;b.value;
    }
};

struct PointeeLess{
    template&lt;typename A,typename B&gt;
    bool operator()(A* a,B* b) {
        return *a&lt;*b;
    }
};

int main() {
    map&lt;Item*, int, PointeeLess&gt; foo;
    foo[new Item(5)] = 5;
    return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1129027</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1129027</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Fri, 01 Sep 2006 12:13:20 GMT</pubDate></item><item><title><![CDATA[Reply to std::map mit eigenem Ordnungsvergleich on Fri, 01 Sep 2006 13:09:02 GMT]]></title><description><![CDATA[<p>warum überhaupt ne item klasse ? es reicht doch wenn man die vergleichsfunktion für den asoziationstyp direkt angibt</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1129105</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1129105</guid><dc:creator><![CDATA[ronny]]></dc:creator><pubDate>Fri, 01 Sep 2006 13:09:02 GMT</pubDate></item><item><title><![CDATA[Reply to std::map mit eigenem Ordnungsvergleich on Fri, 01 Sep 2006 13:10:56 GMT]]></title><description><![CDATA[<p>r0nny schrieb:</p>
<blockquote>
<p>warum überhaupt ne item klasse ? es reicht doch wenn man die vergleichsfunktion für den asoziationstyp direkt angibt</p>
</blockquote>
<p>speed</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1129108</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1129108</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Fri, 01 Sep 2006 13:10:56 GMT</pubDate></item><item><title><![CDATA[Reply to std::map mit eigenem Ordnungsvergleich on Fri, 01 Sep 2006 18:19:26 GMT]]></title><description><![CDATA[<p>templäd schrieb:</p>
<blockquote>
<p>DollyDuplex schrieb:</p>
<blockquote>
<p>Ich dachte jetzt, das geht dann so, aber anscheinend nicht...</p>
</blockquote>
<p>Ich hoffe es antwortet keiner mehr auf solche Fehlerbeschreibungen</p>
</blockquote>
<p>Oh, <em>Verzeihung</em> für das Einbringen einer Floskel ohne offensichtlichen Informationsgehalt. Was ich damit meine ist: Ich hab das ja nicht durch göttliche Eingebung zusammengeschustert, sondern versucht es nachzuschauen. Nur hat es als ich es (so dachte ich) so gemacht hab, wie es da stand, trotzdem nicht funktioniert.</p>
<p>camper schrieb:</p>
<blockquote>
<p>DollyDuplex schrieb:</p>
<blockquote>
<p>Sieht vielleicht jemand den Fehler?</p>
</blockquote>
<p>die kristallkugel haben wir versetzt.<br />
<a href="http://www.lugbz.org/documents/smart-questions_de.html" rel="nofollow">Wie man Fragen richtig stellt</a></p>
</blockquote>
<p>OK, interessant. Entschuldigt, wenn ich dumm Frage; ich bin wohl aus der Übung, weil ich mir die Lösungen meiner Probleme meistens selbst aus dem Allerwertesten leiere.</p>
<p>finix schrieb:</p>
<blockquote>
<p>der Funktor muss nicht von binary_function abgeleitet sein<br />
[...]<br />
&quot;foo*&amp;&quot; ist nicht unbedingt billiger als schlicht zu kopieren.</p>
</blockquote>
<p>Hmjaa, ich hab eben versucht, es so zu machen wie die in der <a href="http://www.sgi.com/tech/stl/less.html" rel="nofollow">Referenz</a> (OK, das mit der binary_function hab ich von wo anders), die haben 'bool operator()(const T&amp; x, const T&amp; y)' und mein 'T' ist eben 'Item*'.</p>
<blockquote>
<p>wer raeumt das bloss wieder auf?</p>
</blockquote>
<p>Das Ganze ist ja nur das Problem aus dem Programm herausgezogen, sonst würde es wohl komplexer... In Wirklichkeit heissen die Sachen natürlich alle anders und die Objekte werden auch wieder gelöscht. :p</p>
<p>Alles in allem: Entschuldigt bitte die Unannehmlichkeiten und Danke für die Lösung!</p>
<p>PS: Es <a href="http://www.sgi.com/tech/stl/Map.html" rel="nofollow">scheint</a> keine Angabe für den Test auf Gleichheit zu geben, verstehe ich das richtig, dass er das über das 'kleiner' löst (x = y wenn weder x &lt; y noch y &lt; x)?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1129327</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1129327</guid><dc:creator><![CDATA[DollyDuplex]]></dc:creator><pubDate>Fri, 01 Sep 2006 18:19:26 GMT</pubDate></item><item><title><![CDATA[Reply to std::map mit eigenem Ordnungsvergleich on Fri, 01 Sep 2006 19:38:55 GMT]]></title><description><![CDATA[<p>DollyDuplex schrieb:</p>
<blockquote>
<p>Hmjaa, ich hab eben versucht, es so zu machen wie die in der <a href="http://www.sgi.com/tech/stl/less.html" rel="nofollow">Referenz</a> (OK, das mit der binary_function hab ich von wo anders), die haben 'bool operator()(const T&amp; x, const T&amp; y)' und mein 'T' ist eben 'Item*'.</p>
</blockquote>
<p>Eben. Der Funktor muss mit konstantem T aufrufbar sein, daher solltest du den Pointer kopieren oder per const-ref übergeben, sprich &quot;Item * const&quot;, nicht &quot;Item const *&quot;.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1129364</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1129364</guid><dc:creator><![CDATA[finix]]></dc:creator><pubDate>Fri, 01 Sep 2006 19:38:55 GMT</pubDate></item><item><title><![CDATA[Reply to std::map mit eigenem Ordnungsvergleich on Sat, 02 Sep 2006 01:19:15 GMT]]></title><description><![CDATA[<p>finix schrieb:</p>
<blockquote>
<p>&quot;Item * const&quot;, nicht &quot;Item const *&quot;.</p>
</blockquote>
<p>OMG, in diesem &quot;Wo gehört das 'const' hin?&quot;-Spiel war ich noch nie sonderlich gut. In meinem Code ist irgendwie so gut wie nix const... <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/1129448</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1129448</guid><dc:creator><![CDATA[DollyDuplex]]></dc:creator><pubDate>Sat, 02 Sep 2006 01:19:15 GMT</pubDate></item></channel></rss>