<?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[Hashtable]]></title><description><![CDATA[<p>Hallo,</p>
<p>kann mir bitte jemand feedback zu meiner implementierten hashtable geben? ich weiss c++11 hat unordered_map, aber ich wollte die hashtable zum ueben selbst implementieren...</p>
<pre><code>#include &lt;iostream&gt;
using namespace std;

class HashEntry {
private:
      int key;
      int value;
public:
      HashEntry(int key, int value) {
            this-&gt;key = key;
            this-&gt;value = value;
      }

      int getKey() {
            return key;
      }

      int getValue() {
            return value;
      }
};

const int TABLE_SIZE = 128;

class HashMap {
private:
      HashEntry **table;
public:
      HashMap() {
            table = new HashEntry*[TABLE_SIZE];
            for (int i = 0; i &lt; TABLE_SIZE; i++)
                  table[i] = NULL;
      }

      int get(int key) {
            int hash = (key % TABLE_SIZE);
            while (table[hash] != NULL &amp;&amp; table[hash]-&gt;getKey() != key)
                  hash = (hash + 1) % TABLE_SIZE;
            if (table[hash] == NULL)
                  return -1;
            else
                  return table[hash]-&gt;getValue();
      }

      void put(int key, int value) {
            int hash = (key % TABLE_SIZE);
            while (table[hash] != NULL &amp;&amp; table[hash]-&gt;getKey() != key)
                  hash = (hash + 1) % TABLE_SIZE;
            if (table[hash] != NULL)
                  delete table[hash];
            table[hash] = new HashEntry(key, value);
      }     

      ~HashMap() {
            for (int i = 0; i &lt; TABLE_SIZE; i++)
                  if (table[i] != NULL)
                        delete table[i];
            delete[] table;
      }
};

int main() {
	// your code goes here

	HashMap m;
	m.put(1, 12);
	m.put(2, 32);
	m.put(2, 10);
	m.put(8, 100);

	cout &lt;&lt; m.get(1) &lt;&lt; endl;
	cout &lt;&lt; m.get(2) &lt;&lt; endl;
	cout &lt;&lt; m.get(8) &lt;&lt; endl;

	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/329932/hashtable</link><generator>RSS for Node</generator><lastBuildDate>Fri, 03 Jul 2026 11:35:41 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/329932.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 14 Dec 2014 21:52:59 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Hashtable on Sun, 14 Dec 2014 21:52:59 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>kann mir bitte jemand feedback zu meiner implementierten hashtable geben? ich weiss c++11 hat unordered_map, aber ich wollte die hashtable zum ueben selbst implementieren...</p>
<pre><code>#include &lt;iostream&gt;
using namespace std;

class HashEntry {
private:
      int key;
      int value;
public:
      HashEntry(int key, int value) {
            this-&gt;key = key;
            this-&gt;value = value;
      }

      int getKey() {
            return key;
      }

      int getValue() {
            return value;
      }
};

const int TABLE_SIZE = 128;

class HashMap {
private:
      HashEntry **table;
public:
      HashMap() {
            table = new HashEntry*[TABLE_SIZE];
            for (int i = 0; i &lt; TABLE_SIZE; i++)
                  table[i] = NULL;
      }

      int get(int key) {
            int hash = (key % TABLE_SIZE);
            while (table[hash] != NULL &amp;&amp; table[hash]-&gt;getKey() != key)
                  hash = (hash + 1) % TABLE_SIZE;
            if (table[hash] == NULL)
                  return -1;
            else
                  return table[hash]-&gt;getValue();
      }

      void put(int key, int value) {
            int hash = (key % TABLE_SIZE);
            while (table[hash] != NULL &amp;&amp; table[hash]-&gt;getKey() != key)
                  hash = (hash + 1) % TABLE_SIZE;
            if (table[hash] != NULL)
                  delete table[hash];
            table[hash] = new HashEntry(key, value);
      }     

      ~HashMap() {
            for (int i = 0; i &lt; TABLE_SIZE; i++)
                  if (table[i] != NULL)
                        delete table[i];
            delete[] table;
      }
};

int main() {
	// your code goes here

	HashMap m;
	m.put(1, 12);
	m.put(2, 32);
	m.put(2, 10);
	m.put(8, 100);

	cout &lt;&lt; m.get(1) &lt;&lt; endl;
	cout &lt;&lt; m.get(2) &lt;&lt; endl;
	cout &lt;&lt; m.get(8) &lt;&lt; endl;

	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2432875</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2432875</guid><dc:creator><![CDATA[stefan2]]></dc:creator><pubDate>Sun, 14 Dec 2014 21:52:59 GMT</pubDate></item><item><title><![CDATA[Reply to Hashtable on Sun, 14 Dec 2014 22:12:50 GMT]]></title><description><![CDATA[<p>Das ist ja schon bei den Grundlagen falsch (und zwar auf ganzer Linie: Der Autor kann weder C++, noch weiß er, wie ein Hash Table funktioniert). Das kommt davon, wenn man von <a href="http://www.algolist.net/Data_structures/Hash_table/Simple_example" rel="nofollow">irgendwelchen dubiosen Quellen</a> abschreibt. Wo ist denn da die Übung für dich und wieso sollten wir Code, den du nicht geschrieben hast, im Details kritisieren? Zusammenklicken aus dem Internet ist nicht Programmieren!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2432876</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2432876</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Sun, 14 Dec 2014 22:12:50 GMT</pubDate></item><item><title><![CDATA[Reply to Hashtable on Mon, 15 Dec 2014 06:00:27 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/19375">@SeppJ</a>:<br />
Kennst du denn eine Seite, die man mit ruhigem Gewissen empfehlen kann, um sich mit Datenstrukturen und Algorithmen vertraut zu machen? Google kann ja leider nicht sagen wie gut die gezeigten Treffer vom Inhalt sind. Also geht so eine Suche nur über Empfehlung von Profis.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2432882</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2432882</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Mon, 15 Dec 2014 06:00:27 GMT</pubDate></item><item><title><![CDATA[Reply to Hashtable on Mon, 15 Dec 2014 06:05:48 GMT]]></title><description><![CDATA[<p>Citizen42 schrieb:</p>
<blockquote>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/19375">@SeppJ</a>:<br />
Kennst du denn eine Seite, die man mit ruhigem Gewissen empfehlen kann, um sich mit Datenstrukturen und Algorithmen vertraut zu machen?</p>
</blockquote>
<p>Ich bin eigentlich oft recht zufrieden mit Wikipedia.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2432884</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2432884</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Mon, 15 Dec 2014 06:05:48 GMT</pubDate></item><item><title><![CDATA[Reply to Hashtable on Mon, 15 Dec 2014 06:24:52 GMT]]></title><description><![CDATA[<p>SeppJ schrieb:</p>
<blockquote>
<p>Citizen42 schrieb:</p>
<blockquote>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/19375">@SeppJ</a>:<br />
Kennst du denn eine Seite, die man mit ruhigem Gewissen empfehlen kann, um sich mit Datenstrukturen und Algorithmen vertraut zu machen?</p>
</blockquote>
<p>Ich bin eigentlich oft recht zufrieden mit Wikipedia.</p>
</blockquote>
<p>Aber die englische Version, versteht sich.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2432885</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2432885</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Mon, 15 Dec 2014 06:24:52 GMT</pubDate></item><item><title><![CDATA[Reply to Hashtable on Mon, 15 Dec 2014 06:36:29 GMT]]></title><description><![CDATA[<p>Nathan schrieb:</p>
<blockquote>
<p>SeppJ schrieb:</p>
<blockquote>
<p>Citizen42 schrieb:</p>
<blockquote>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/19375">@SeppJ</a>:<br />
Kennst du denn eine Seite, die man mit ruhigem Gewissen empfehlen kann, um sich mit Datenstrukturen und Algorithmen vertraut zu machen?</p>
</blockquote>
<p>Ich bin eigentlich oft recht zufrieden mit Wikipedia.</p>
</blockquote>
<p>Aber die englische Version, versteht sich.</p>
</blockquote>
<p>Meistens. In den letzten Jahren hat die deutsche Wikipedia meiner Meinung nach bei Informatikthemen ganz gut aufgeholt. (Und bei Autothemen ist sie sowieso die beste <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>
<p>Citizen42 schrieb:</p>
<blockquote>
<p>Google kann ja leider nicht sagen wie gut die gezeigten Treffer vom Inhalt sind.</p>
</blockquote>
<p>Aber zumindest bei dem hier gezeigten Code sollten alle Alarmglocken schrillen, weil allein schon die technische Umsetzung fehlerhaft ist. Wer nicht weiß, wie in C++ eine Containerklasse aufgebaut ist, dem traut man besser auch nicht bei der Programmlogik.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2432887</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2432887</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Mon, 15 Dec 2014 06:36:29 GMT</pubDate></item><item><title><![CDATA[Reply to Hashtable on Mon, 15 Dec 2014 07:22:19 GMT]]></title><description><![CDATA[<p>Ok, also kann man wirklich Wikipedia in der Hinsicht trauen. Das ist doch schon mal eine Hausnummer.</p>
<p>Vielleicht könnte Jemand hier erläutern, wieso der oben gezeigte Code schlecht ist. Mir fällt jetzt nur das Hantieren mit rohen Zeigern auf, aber er sagte ja auch, dass er es selbst implementieren wollte, also dass er keine STL in solch einem Falle nutzen kann.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2432890</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2432890</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Mon, 15 Dec 2014 07:22:19 GMT</pubDate></item><item><title><![CDATA[Reply to Hashtable on Mon, 15 Dec 2014 08:19:33 GMT]]></title><description><![CDATA[<ul>
<li>Anfängerfehler: Regel der großen Drei nicht beachtet.</li>
<li>Ungeschickt: Warum Werte nicht direkt beim new selbst mit 0 initialisieren?</li>
<li>Laienhaft: Von Initialisierungslisten haben wir noch nie gehört, oder?</li>
<li>Designfehler: Warum ist das kein Template? Mindestens key, value und Hashfunktion gehören hier frei wählbar und es wäre dazu nicht mehr als eine einzige Zeile <code>template&lt;....&gt;</code> am Anfang nötig.</li>
<li>Designfehler: Warum sind Größenangaben int?</li>
<li>Designfehler: Warum sind da globale Konstanten?</li>
<li>Designfehler: Warum gibt die get-Methode -1 bei Fehler zurück? Was ist, wenn mein value -1 ist?</li>
<li>Designfehler: Und wenn mehr als TABLE_SIZE Einträge gemacht werden?</li>
<li>Designfehler: Warum ist bei dieser Form der Implementierung table überhaupt vom Typ Zeiger auf Zeiger auf Hashelement? Die Zeigereigenschaft wird nur benutzt, um festzustellen, ob ein Element NULL ist oder nicht. Das wäre doch auch einfacher gegangen. Man hat den Eindruck, der Autor hat mal Hastables mit Zeigern auf Zeigern gesehen, aber nicht so richtig verstanden, wie sie funktionieren.</li>
<li>Designfehler: Die Implementierung ist nicht nur auf TABLE_SIZE Einträge beschränkt, sie führt sogar noch nahezu absichtlich Kollisionen herbei, die normalerweise gar nicht da wären!</li>
<li>Anfängerfehler: Was passiert denn, wenn man NULL deletet?</li>
<li>Ungeschickt: Warum überschreibt das put bei einem doppelten key nicht einfach den alten value?</li>
<li>Doppelt ungeschickt: Was passiert, wenn das new im put wirft?</li>
<li>Designfehler: Kann man später auch mal was machen mit dem Hashtable? Ich will ja nicht nur Einfügen und Zugreifen. Bei dem Design wird es zum Beispiel zwischen schwierig bis unmöglich, einen Wert zu entfernen. Das ist nicht die einzige Aktion oder Komfortfunktion, die mit dem Design und/oder der technischen Umsetzung verbaut wurde.</li>
</ul>
<p>Das ist jetzt nur mal eben schnell quer gelesen, so genau habe ich den Code gar nicht untersuchen müssen, um hier mehrere Seiten im Editorfenster zu füllen.</p>
<blockquote>
<p>aber er sagte ja auch, dass er es selbst implementieren wollte, also dass er keine STL in solch einem Falle nutzen kann.</p>
</blockquote>
<p>Man kann einen Hashtable doch wunderbar als vector von vector oder als vector von (multi-)set implementieren. Dann lernt man wunderbar viel über Hashtables, aber muss sich nicht mit Details über Speicherverwaltung rumschlagen, die mit Hashtablealgorithmen erst einmal nichts zu tun haben.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2432895</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2432895</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Mon, 15 Dec 2014 08:19:33 GMT</pubDate></item><item><title><![CDATA[Reply to Hashtable on Mon, 15 Dec 2014 09:34:27 GMT]]></title><description><![CDATA[<blockquote>
<p>Doppelt ungeschickt: Was passiert, wenn das new im put wirft?</p>
</blockquote>
<p>Wie soll man das umgehen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2432907</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2432907</guid><dc:creator><![CDATA[trycatchnix]]></dc:creator><pubDate>Mon, 15 Dec 2014 09:34:27 GMT</pubDate></item><item><title><![CDATA[Reply to Hashtable on Mon, 15 Dec 2014 09:42:09 GMT]]></title><description><![CDATA[<p>trycatchnix schrieb:</p>
<blockquote>
<blockquote>
<p>Doppelt ungeschickt: Was passiert, wenn das new im put wirft?</p>
</blockquote>
<p>Wie soll man das umgehen?</p>
</blockquote>
<p>Erst allozieren, dann die Hash Table anfassen. Damit bleibt die Hash Table intakt.</p>
<p>Edit:<br />
Typos o.O</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2432909</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2432909</guid><dc:creator><![CDATA[DocShoe]]></dc:creator><pubDate>Mon, 15 Dec 2014 09:42:09 GMT</pubDate></item><item><title><![CDATA[Reply to Hashtable on Mon, 15 Dec 2014 10:00:29 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/19375">@SeppJ</a>:<br />
Das sind ja fast mehr Fehler als der Code lang ist <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="😃"
    /> Manchmal kann ich die Leute verstehen, die einen Bogen ums C++-Lernen machen <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2432918</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2432918</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Mon, 15 Dec 2014 10:00:29 GMT</pubDate></item><item><title><![CDATA[Reply to Hashtable on Mon, 15 Dec 2014 10:16:31 GMT]]></title><description><![CDATA[<p>Citizen42 schrieb:</p>
<blockquote>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/19375">@SeppJ</a>:<br />
Das sind ja fast mehr Fehler als der Code lang ist <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="😃"
    /> Manchmal kann ich die Leute verstehen, die einen Bogen ums C++-Lernen machen <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
</blockquote>
<p>In anderen Sprachen waeren es 3 bis maximal 5 Problemfaelle weniger, dafuer aber vielleicht auch wieder neue dazu. Das Hauptproblem ist eine vom voellig falsche designte Hashtable.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2432921</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2432921</guid><dc:creator><![CDATA[Marthog]]></dc:creator><pubDate>Mon, 15 Dec 2014 10:16:31 GMT</pubDate></item><item><title><![CDATA[Reply to Hashtable on Sat, 27 Dec 2014 23:39:46 GMT]]></title><description><![CDATA[<p>wo finde ich eine c++ hash table implementierung, die nicht gerade von templates ueberfuellt ist?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2434911</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2434911</guid><dc:creator><![CDATA[coder007]]></dc:creator><pubDate>Sat, 27 Dec 2014 23:39:46 GMT</pubDate></item><item><title><![CDATA[Reply to Hashtable on Sun, 28 Dec 2014 00:52:42 GMT]]></title><description><![CDATA[<p>coder007 schrieb:</p>
<blockquote>
<p>wo finde ich eine c++ hash table implementierung, die nicht gerade von templates ueberfuellt ist?</p>
</blockquote>
<p>Eine ernsthafte Implementierung? Wohl nirgendwo, denn warum sollte man das jemals ohne Templates machen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2434916</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2434916</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Sun, 28 Dec 2014 00:52:42 GMT</pubDate></item><item><title><![CDATA[Reply to Hashtable on Sun, 28 Dec 2014 09:06:08 GMT]]></title><description><![CDATA[<p>Also es wurde ja auch nach Quellen gefragt, ich erinnere mich daran, dass bei den 6.006 MIT open courseware Videos eins zumindest teilweise Hashing bzw. Hashtables behandelt hat.</p>
<p>Achja, hier der Link: <a href="https://www.youtube.com/watch?v=oFBEj6wE_Jw" rel="nofollow">https://www.youtube.com/watch?v=oFBEj6wE_Jw</a></p>
<p>Ich kann die Qualität der hier vorgestellten Dinge nicht beurteilen (sollte ich dann den Link posten? Ich denke schon, denn:) das ist von einer recht bekannten Universität, und ich hab schonmal irgendo eine Empfehlung für diese Videos gelesen, also daneben wird das vermutlich nicht sein.</p>
<p>P.S.: Wenn es um das Interface Design geht, könnte man sich ja einfach an der std::unordered_map orientieren...</p>
<p>LG</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2434933</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2434933</guid><dc:creator><![CDATA[hardware]]></dc:creator><pubDate>Sun, 28 Dec 2014 09:06:08 GMT</pubDate></item><item><title><![CDATA[Reply to Hashtable on Sun, 28 Dec 2014 14:15:06 GMT]]></title><description><![CDATA[<p>ich suche keine ernsthafte implementierung, sondern nur wie man es im umfang eines interviews coden kann...</p>
<p>folgendes ist recht brauchbar:<br />
<a href="http://blog.aozturk.me/simple-hash-map-hash-table-implementation-in-c" rel="nofollow">http://blog.aozturk.me/simple-hash-map-hash-table-implementation-in-c</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2434962</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2434962</guid><dc:creator><![CDATA[coder007]]></dc:creator><pubDate>Sun, 28 Dec 2014 14:15:06 GMT</pubDate></item><item><title><![CDATA[Reply to Hashtable on Sun, 28 Dec 2014 16:48:24 GMT]]></title><description><![CDATA[<p>Wie wärs damit:</p>
<pre><code class="language-cpp">#include &lt;memory&gt;
#include &lt;cassert&gt;
#include &lt;unordered_set&gt;
#include &lt;vector&gt;

size_t hash(int x) { return x; }

template &lt;typename T&gt;
class hash_table {
  typedef std::size_t key_t;

  struct slist_node {
    slist_node(key_t key, T value)
      : key(key), next(nullptr), value(std::move(value)) {}
    key_t key;
    slist_node *next;
    T value;
  };

  slist_node *find(slist_node *n, key_t key, T const&amp; x) const {
    for (; n; n=n-&gt;next)
      if (n-&gt;key == key &amp;&amp; n-&gt;value == x)
        return n;
    return nullptr;
  }

  std::vector&lt;slist_node*&gt; table;
  size_t size_ = 0;

public:
  hash_table(size_t n = 13)
    : table(n, nullptr) {}
  hash_table(hash_table const&amp; o)
    : hash_table(o.table.size(), o) {}
  hash_table(hash_table&amp;&amp; o)
    : hash_table(o.table.size(), std::move(o)) {}
  hash_table(size_t n, hash_table o)
    : table(n, nullptr) {
    for (auto *n : table) {
      for (; n; n=n-&gt;next) {
        insert(n-&gt;key, std::move(n-&gt;value));
      }
    }
  }
  ~hash_table() {
    for (auto *n : table) {
      while (n) {
        auto *next = n-&gt;next;
        delete n;
        n = next;
      }
    }
  }
  friend void swap(hash_table&amp; a, hash_table&amp; b) {
    std::swap(a.table, b.table);
  }
  hash_table&amp; operator=(hash_table o) {
    swap(*this, o);
  }
  void rehash(size_t n) {
    hash_table o(n, *this);
    swap(o, *this);
  }

  bool contains(T const&amp; x) const {
    auto key = static_cast&lt;key_t&gt;(hash(x));
    return find(table[key%table.size()], key, x);
  }

  bool insert(T x) {
    auto key = static_cast&lt;key_t&gt;(hash(x));
    auto&amp; old_nd = table[key%table.size()];
    if (find(old_nd, key, x))
      return false;
    auto new_nd = std::make_unique&lt;slist_node&gt;(key, std::move(x));
    new_nd-&gt;next = old_nd;
    old_nd = new_nd.release();
    ++size_;
    return true;
  }        

  size_t size() const { return size_; }
};

int main() {
  hash_table&lt;int&gt; h1;
  std::unordered_set&lt;int&gt; h2;

  srand(0);
  for (int i=0; i&lt;1000; ++i) {
    int x = rand()%100;
    assert(h1.insert(x) == h2.insert(x).second);
    assert(h1.size() == h2.size());
  }
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2435021</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2435021</guid><dc:creator><![CDATA[tabulus]]></dc:creator><pubDate>Sun, 28 Dec 2014 16:48:24 GMT</pubDate></item><item><title><![CDATA[Reply to Hashtable on Sun, 28 Dec 2014 18:27:27 GMT]]></title><description><![CDATA[<p>coder007 schrieb:</p>
<blockquote>
<p>ich suche keine ernsthafte implementierung, sondern nur wie man es im umfang eines interviews coden kann...</p>
</blockquote>
<p>Wenn du in einem Interview kein Template hin bekommst, wäre ich schon ziemlich schwer enttäuscht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2435046</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2435046</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Sun, 28 Dec 2014 18:27:27 GMT</pubDate></item><item><title><![CDATA[Reply to Hashtable on Mon, 29 Dec 2014 11:05:19 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/19375">@SeppJ</a>: ich meinte template spielereien wie zb. in boost: <a href="http://www.boost.org/doc/libs/1_46_0/boost/unordered/unordered_map.hpp" rel="nofollow">http://www.boost.org/doc/libs/1_46_0/boost/unordered/unordered_map.hpp</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2435119</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2435119</guid><dc:creator><![CDATA[coder007]]></dc:creator><pubDate>Mon, 29 Dec 2014 11:05:19 GMT</pubDate></item><item><title><![CDATA[Reply to Hashtable on Mon, 29 Dec 2014 11:07:39 GMT]]></title><description><![CDATA[<p>eine map in der standard lib hat ja auch iteratoren, welche man implementieren muss... da ist es schon einfacher eine hash table zu implementieren, welche das core konzept implementiert...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2435120</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2435120</guid><dc:creator><![CDATA[coder007]]></dc:creator><pubDate>Mon, 29 Dec 2014 11:07:39 GMT</pubDate></item><item><title><![CDATA[Reply to Hashtable on Mon, 29 Dec 2014 11:32:46 GMT]]></title><description><![CDATA[<p>coder007 schrieb:</p>
<blockquote>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/19375">@SeppJ</a>: ich meinte template spielereien wie zb. in boost: <a href="http://www.boost.org/doc/libs/1_46_0/boost/unordered/unordered_map.hpp" rel="nofollow">http://www.boost.org/doc/libs/1_46_0/boost/unordered/unordered_map.hpp</a></p>
</blockquote>
<p>Wo sind denn da Template Spielereien? Ist doch noch alles straight-forward.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2435123</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2435123</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Mon, 29 Dec 2014 11:32:46 GMT</pubDate></item></channel></rss>