<?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[boost::hash, schönere variante?]]></title><description><![CDATA[<p>Hi,</p>
<p>ich möchte gerne in meinen klassen eine &quot;get_hash ()&quot;-methode einbauen und dafür boost benutzen, doch nicht jedes mal wenn ich den hash haben will einen boost::hash erzeugen muss.</p>
<p>Ich hab es so derzeit gemacht, doch diese lösung gefällt mir nicht (2. hash_value-methode)</p>
<pre><code class="language-cpp">#include &lt;boost/functional/hash.hpp&gt;
#include &lt;cassert&gt;
#include &lt;iostream&gt;
#include &lt;string&gt;

class point
{
    int x;
    int y;
public:
    point() : x(0), y(0) {}
    point(int x, int y) : x(x), y(y) {}

    bool operator==(point const&amp; other) const
    {
        return x == other.x &amp;&amp; y == other.y;
    }

    friend std::size_t hash_value(point const&amp; p)
    {
        std::wstring value = L&quot;hallo welt &quot;;
        std::size_t seed = 0;
        boost::hash_combine(seed, p.x);
        boost::hash_combine(seed, p.y);
        boost::hash_combine(seed, value);

        return seed;
    }

    std::size_t hash_value (void) const
    {
        boost::hash&lt;point&gt; hasher;
        return (hasher (*this));
    }
};

int main()
{
    point p1(0, 0);

    std::cout &lt;&lt; p1.hash_value () &lt;&lt; std::endl;

    return 0;
}
</code></pre>
<p>Jemand eine bessere idee, oder kann ich den hash nur bekommen, wenn ich einen &quot;hasher&quot; benutze?</p>
<p>Viele Grüße!<br />
Booster</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/224578/boost-hash-schönere-variante</link><generator>RSS for Node</generator><lastBuildDate>Fri, 02 Oct 2026 08:47:09 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/224578.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 11 Oct 2008 20:38:45 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to boost::hash, schönere variante? on Sat, 11 Oct 2008 20:38:45 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>ich möchte gerne in meinen klassen eine &quot;get_hash ()&quot;-methode einbauen und dafür boost benutzen, doch nicht jedes mal wenn ich den hash haben will einen boost::hash erzeugen muss.</p>
<p>Ich hab es so derzeit gemacht, doch diese lösung gefällt mir nicht (2. hash_value-methode)</p>
<pre><code class="language-cpp">#include &lt;boost/functional/hash.hpp&gt;
#include &lt;cassert&gt;
#include &lt;iostream&gt;
#include &lt;string&gt;

class point
{
    int x;
    int y;
public:
    point() : x(0), y(0) {}
    point(int x, int y) : x(x), y(y) {}

    bool operator==(point const&amp; other) const
    {
        return x == other.x &amp;&amp; y == other.y;
    }

    friend std::size_t hash_value(point const&amp; p)
    {
        std::wstring value = L&quot;hallo welt &quot;;
        std::size_t seed = 0;
        boost::hash_combine(seed, p.x);
        boost::hash_combine(seed, p.y);
        boost::hash_combine(seed, value);

        return seed;
    }

    std::size_t hash_value (void) const
    {
        boost::hash&lt;point&gt; hasher;
        return (hasher (*this));
    }
};

int main()
{
    point p1(0, 0);

    std::cout &lt;&lt; p1.hash_value () &lt;&lt; std::endl;

    return 0;
}
</code></pre>
<p>Jemand eine bessere idee, oder kann ich den hash nur bekommen, wenn ich einen &quot;hasher&quot; benutze?</p>
<p>Viele Grüße!<br />
Booster</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1597607</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1597607</guid><dc:creator><![CDATA[booster]]></dc:creator><pubDate>Sat, 11 Oct 2008 20:38:45 GMT</pubDate></item><item><title><![CDATA[Reply to boost::hash, schönere variante? on Sat, 11 Oct 2008 21:34:27 GMT]]></title><description><![CDATA[<p>Wo liegt denn dein Problem?<br />
Benutz' einfach Boost.Hash wie es gemacht ist und es funktioniert alles wie es soll ;).</p>
<p>Wofür braucht dein Objekt denn überhaupt eine Methode, die seinen Hash zurückgibt? Sieht mir nicht nach so tollem Design aus…</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1597627</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1597627</guid><dc:creator><![CDATA[.filmor]]></dc:creator><pubDate>Sat, 11 Oct 2008 21:34:27 GMT</pubDate></item><item><title><![CDATA[Reply to boost::hash, schönere variante? on Sat, 11 Oct 2008 21:38:26 GMT]]></title><description><![CDATA[<p>ah, du findest es also &quot;eleganter&quot; jedesmal wenn ich einen hash erfrage immer ein objekt von boost::hash&lt;T&gt; anzulegen? selbst wenn ich in einer methode von 10 verschiedenen objekten die hashs brauche und dafür 10 von den hash-dingern anlegen muss? also wenn du das &quot;schöner&quot; findest, ich hab da lieber einfach ein objekt.hash_value (); und hab ruhe und platz gespart.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1597628</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1597628</guid><dc:creator><![CDATA[booster]]></dc:creator><pubDate>Sat, 11 Oct 2008 21:38:26 GMT</pubDate></item><item><title><![CDATA[Reply to boost::hash, schönere variante? on Sat, 11 Oct 2008 22:11:35 GMT]]></title><description><![CDATA[<p>booster schrieb:</p>
<blockquote>
<p>ah, du findest es also &quot;eleganter&quot; jedesmal wenn ich einen hash erfrage immer ein objekt von boost::hash&lt;T&gt; anzulegen?...</p>
</blockquote>
<p>Also ich weiß nicht, was daran komisch klingen soll.<br />
&quot;Einen Hash erfragen&quot; und &quot;einen hash anlegen&quot; sind doch genau die beiden Sichtweisen des von Dir geforderten Vorgangs.<br />
Genauso wie &quot;einen Namensstring erfragen&quot; und &quot;einen Namensstring anlegen&quot;....<br />
... und mit &quot;Performance&quot; würde ich nie argumentieren ohne konkrete Messungen.</p>
<p>Hast Du eigentlich Erfahrungen mit &quot;Funktoren&quot;/&quot;Funktionsobjekten&quot; ?<br />
Das sind zwar semantisch betrachtet Objekte, im Compilat sind es aber einfach Funktionen, die aufgerufen werden. Ergo: &quot;Objekt anlegen&quot; muss sich performancetechnisch nicht anders verhalten als &quot;Funktion aufrufen&quot;.</p>
<p>Gruß,</p>
<p>Simon2.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1597639</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1597639</guid><dc:creator><![CDATA[Simon2]]></dc:creator><pubDate>Sat, 11 Oct 2008 22:11:35 GMT</pubDate></item><item><title><![CDATA[Reply to boost::hash, schönere variante? on Sat, 11 Oct 2008 22:21:19 GMT]]></title><description><![CDATA[<p>what the hell are you talking about?<br />
ich versteh nur bahnhof</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1597640</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1597640</guid><dc:creator><![CDATA[booster]]></dc:creator><pubDate>Sat, 11 Oct 2008 22:21:19 GMT</pubDate></item><item><title><![CDATA[Reply to boost::hash, schönere variante? on Sat, 11 Oct 2008 22:36:23 GMT]]></title><description><![CDATA[<p>Ist auch wurscht, wenn du 10 Hashes haben willst wirst du aber sicherlich nicht 10 Objekte anlegen. boost::hash ist ein Funktionsobjekt, du legst also quasi damit eine Funktion an:</p>
<pre><code class="language-cpp">boost::hash&lt;point&gt; hasher;

std::vector&lt;std::size_t&gt; da_hashes;
for (std::vector&lt;point&gt;::const_iterator i = v.begin(); i != v.end(); ++i)
    da_hashes.push_back(hasher(*i));
</code></pre>
<p>An dem Beispiel kannst du auch sehr schön sehen, warum das Objekt sinnvoll sein könnte, damit kannst du nämlich richtig toll STL machen:</p>
<pre><code class="language-cpp">// Initialisierung wie oben, aber:
std::transform(v.begin(), v.end(), std::back_inserter(da_hashes), hasher);
// oder gleich mit den Daten initialisieren:
std::vector&lt;std::size_t&gt; da_nu_hashes (boost::make_transform_iterator(v.begin(), hasher), boost::make_transform_iterator(v.end(), hasher);
</code></pre>
<p>Und es ist nunmal nicht grundlegende Eigenschaft eines Objektes, dass es einen Hash von sich selbst erzeugen kann, das hat einfach keine öffentliche Methode zu sein.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1597646</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1597646</guid><dc:creator><![CDATA[.filmor]]></dc:creator><pubDate>Sat, 11 Oct 2008 22:36:23 GMT</pubDate></item><item><title><![CDATA[Reply to boost::hash, schönere variante? on Sat, 11 Oct 2008 22:39:11 GMT]]></title><description><![CDATA[<p>ich meinte mit 10 objekten nicht 10 objekte von point sondern von objekte von 10 unterschiedlichen klassen - habe mich etwas undeutlich ausgedrückt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1597647</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1597647</guid><dc:creator><![CDATA[booster]]></dc:creator><pubDate>Sat, 11 Oct 2008 22:39:11 GMT</pubDate></item><item><title><![CDATA[Reply to boost::hash, schönere variante? on Sun, 12 Oct 2008 12:11:18 GMT]]></title><description><![CDATA[<p>booster schrieb:</p>
<blockquote>
<p>ich meinte mit 10 objekten nicht 10 objekte von point sondern von objekte von 10 unterschiedlichen klassen - habe mich etwas undeutlich ausgedrückt.</p>
</blockquote>
<p>Dadurch wird's mir jetzt auch nicht klarer. Wenn Du von 10 point-Objekten die Hashes haben willst ... welche &quot;..objekte von 10 unterschiedlichen klassen..&quot; (BTW: Shift-Taste kaputt ? <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="😉"
    /> ) musst Du dann anlegen ?</p>
<p>Gruß,</p>
<p>Simon2.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1597759</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1597759</guid><dc:creator><![CDATA[Simon2]]></dc:creator><pubDate>Sun, 12 Oct 2008 12:11:18 GMT</pubDate></item></channel></rss>