title: C++ unordered_map self defined hash function collision
body: <p>Below code is for counting number of lines in a plane for different slope value. It is recommended to use a pair of x-axis and y-axis positions to denote the slope of line, b/c directly calculating the division <code>y / x</code> will have float precision issue. All x and y positions are integers.</p>
  
  <p>Although Method I is working in test code, there are still something unclear to me:</p>
  
  <p>1) For method I, pair {5, 3} and {3, 5} will have the same hash value (x ^ y), but these two lines have different slope! Why does not it cause the problem of considering both lines the same slope? Or the hash function value only determines the slot to be hashed, while comparing the equivalence of actual pair value determines whether to count them as equal?</p>
  
  <p>2) Since the pair {5, 3} and {3, 5} will be hashed into the same slot, and there are lots of other similar collisions like {a, b} and {b, a}. Why does the collision hash table still produces correct final result? </p>
  
  <p>3) XOR for negative integers is fine, right? Is there a better hash function we usually use here to avoid high collision?</p>
  
  <pre><code>struct hashfunc
  {
      //Method I:
      size_t operator() (const pair&lt;int,int&gt;&amp; l) const
      { return l.first ^ l.second; }   
  
      //Method II is WRONG: can NOT left shift negative int!!
      size_t operator() (const pair&lt;int, int&gt;&amp; l) const {
           return l.first &lt;&lt; 32 | l.second; 
      }
  };
  
  unordered_map&lt; pair&lt; int,int &gt;, int, hashfunc&gt; lines;
  </code></pre>