# HashTable

**URL:** <https://forum.juce.com/t/hashtable/510>\
**Category:** Useful Tools and Components\
**Created:** [November 19, 2005, 4:03am UTC](https://forum.juce.com/t/hashtable/510 "2005-11-19T04:03:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jtxx000](https://avatars.discourse-cdn.com/v4/letter/j/b5a626/32.png) [@jtxx000](https://forum.juce.com/u/jtxx000)\
**Post date:** [November 19, 2005, 4:03am UTC](https://forum.juce.com/t/hashtable/510/1 "2005-11-19T04:03:29Z")

</div>

[Here](http://www.annoware.com/downloads/HashTable.h)’s a hash table with linked lists to resolve collisions. It hasn’t been thoroughly tested but it appears to work correctly. Here’s an example of how to use it:

```auto
HashTable<Image*> table;
table.set(T("giraffe"),giraffeImg);
table.set(T("dog"),dogImg);
table.set(T("emu"),emuImg);

table[T("giraffe")] >> giraffeImg
table[T("dog")] >> dogImg
table[T("emu")] >> emuImg
table[T("moose")] >> 0
```

---

<div class="post-metadata">

**Author:** ![OvermindDL1](https://avatars.discourse-cdn.com/v4/letter/o/a8b319/32.png) [@OvermindDL1](https://forum.juce.com/u/OvermindDL1)\
**Post date:** [November 19, 2005, 10:50am UTC](https://forum.juce.com/t/hashtable/510/2 "2005-11-19T10:50:44Z")

</div>

There is also an excellent one that comes with the standard\_extension set. 🙂

---

<div class="post-metadata">

**Author:** ![jtxx000](https://avatars.discourse-cdn.com/v4/letter/j/b5a626/32.png) [@jtxx000](https://forum.juce.com/u/jtxx000)\
**Post date:** [November 19, 2005, 2:10pm UTC](https://forum.juce.com/t/hashtable/510/3 "2005-11-19T14:10:50Z")

</div>

Got a link? Is it written in JUCE?

---

<div class="post-metadata">

**Author:** ![OvermindDL1](https://avatars.discourse-cdn.com/v4/letter/o/a8b319/32.png) [@OvermindDL1](https://forum.juce.com/u/OvermindDL1)\
**Post date:** [November 20, 2005, 1:50am UTC](https://forum.juce.com/t/hashtable/510/4 "2005-11-20T01:50:09Z")

</div>

Don’t need a link, it comes with the compiler, it’s in the std set, or std\_ext, depending on the compiler.

---

<div class="post-metadata">

**Author:** ![jtxx000](https://avatars.discourse-cdn.com/v4/letter/j/b5a626/32.png) [@jtxx000](https://forum.juce.com/u/jtxx000)\
**Post date:** [November 20, 2005, 2:15am UTC](https://forum.juce.com/t/hashtable/510/5 "2005-11-20T02:15:14Z")

</div>

hmm… didn’t know there was a hash table that came with c++. if i did, i probably wouldn’t have coded my own. 😛

---

<div class="post-metadata">

**Author:** ![OvermindDL1](https://avatars.discourse-cdn.com/v4/letter/o/a8b319/32.png) [@OvermindDL1](https://forum.juce.com/u/OvermindDL1)\
**Post date:** [November 20, 2005, 2:41am UTC](https://forum.juce.com/t/hashtable/510/6 "2005-11-20T02:41:13Z")

</div>

The one that comes with it is pretty powerful. It has a hashing function that provides for an extremely low chance of collisions. It has a specalized template for strings to help prevent collisions. When you define the class you can pass in your own hashing function in the template parameters if you have a more specialized needs, etc… It is immensly powerful.

The best documentation on the current stl and the stl\_ext libraries is at [http://www.sgi.com/tech/stl/table\_of\_contents.html](http://www.sgi.com/tech/stl/table_of_contents.html) and if you search for ‘hash’, you will find things from hash\_set’s, hash\_map’s, multi versions of each, and a generic ‘hash’ function which is used by the afore mentioned ones.

Also, if anyone reads this thread, you should take a look at the rope class, if you want a very fast string function to handle strings that can be arbitratilly large and still remain fast. It is unsuitable for anything short however. 🙂

EDIT: Here is a code snippet of how to use the hash\_map that comes with C++, with the key being a string, and the value being an int.

[code]eqstr  
{  
bool operator()(const char\* s1, const char\* s2) const  
{  
return strcmp(s1, s2) == 0;  
}  
};

int main()  
{  
hash\_map\<const char\*, int, hash\<const char\*\>, eqstr\> months;

months[“january”] = 31;  
months[“february”] = 28;  
months[“march”] = 31;  
months[“april”] = 30;  
months[“may”] = 31;  
months[“june”] = 30;  
months[“july”] = 31;  
months[“august”] = 31;  
months[“september”] = 30;  
months[“october”] = 31;  
months[“november”] = 30;  
months[“december”] = 31;

cout \<\< "september -\> " \<\< months[“september”] \<\< endl;  
cout \<\< "april -\> " \<\< months[“april”] \<\< endl;  
cout \<\< "june -\> " \<\< months[“june”] \<\< endl;  
cout \<\< "november -\> " \<\< months[“november”] \<\< endl;  
}[/code]

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [May 12, 2017, 9:51am UTC](https://forum.juce.com/t/hashtable/510/7 "2017-05-12T09:51:52Z")

</div>

Closed on May 12, 2017, 9:51am UTC


