title: C++: Why isn't my hash function for the unordered_map<char*, vector<int>> working?
body: <p>I want to create an unordered_map which has a <code>char*</code> as a key and a <code>vector&lt;int&gt;</code> as a <code>value</code>. I learned from previous questions that no hash function for <code>char*</code>  is provided by the STL.</p>
  
  <p>I took the first implementation from this site: <a href="http://www.cse.yorku.ca/~oz/hash.html" rel="nofollow noreferrer">http://www.cse.yorku.ca/~oz/hash.html</a></p>
  
  <p>So having my <code>main.cpp</code> file I inserted the following code:</p>
  
  <pre><code>namespace std
  {
     template&lt;&gt;
     struct hash&lt;char*&gt;: public std::unary_function&lt;char *, size_t&gt;
     {
        size_t operator()(char * str) const{
        size_t hash = 5381;
        int c;
  
        while(c = *str++)
          hash = ((hash &lt;&lt; 5) + hash) + c; /* hash * 33 + c */
  
        return hash;
  
       }
    };
  }
  </code></pre>
  
  <p>Then I created an unordered_map variable:</p>
  
  <pre><code>std::unordered_map&lt;char *, vector&lt;int&gt;&gt; test;
  </code></pre>
  
  <p>However if I insert the value "temp" twice by doing this:</p>
  
  <pre><code>std::unordered_map&lt;char *, vector&lt;int&gt;&gt; test;
  char *t1 = new char[5];
  strcpy(t1, "temp");
  char *t2 = new char[5];
  strcpy(t2, "temp");
  vector&lt;int&gt;&amp; ptr = test[t1];
  ptr.push_back(0);
  vector&lt;int&gt;&amp; ptr2 = test[t2];
  ptr2.push_back(1);
  </code></pre>
  
  <p>the final map instead of having one "temp" key with a vector of size two where each element of the vector is either 0 or 1 it has two keys with the name "temp" and in each key a vector of size 1.</p>
  
  <p>here is detailed picture:
  <img src="https://i.stack.imgur.com/hxWQU.png" alt="enter image description here"></p>
  
  <p>how can I avoid this from happening? thank you in advance</p>