title: STL map custom comparator
body: <p>I am trying to use a user defined type as a map key with a custom comparator as follows. </p>
  
  <pre><code>#include &lt;map&gt;
  #include &lt;iostream&gt;
  
  class RangeKey {
    public:
      int start;
      int end;
  
      RangeKey(int start, int end) : start(start), end(end) {
  
      }
  
      bool withinRange(int value, bool inclusive) const {
        if (inclusive) {
          return (value &gt;= start &amp;&amp; value &lt;= end);
      } else {
        return (value &gt; start &amp;&amp; value &lt; end);
      }
    }
  
    bool overlapsWith(const RangeKey&amp; r) const {
      if (r.withinRange(start, true) ||
          r.withinRange(end, true) ||
          (start &lt; r.start &amp;&amp; end &gt; r.end)) {
        return true;
      }
      return false;
    }
  
  };
  
  class RangeKeyComparator {
    public:
      bool operator()(const RangeKey&amp; a, const RangeKey&amp; b) const {
        if (a.overlapsWith(b)) {
          return true;
        } else {
          return a.start &lt; b.start;
        }
      }
  };
  
  int main() {
    std::map&lt;RangeKey, int, RangeKeyComparator&gt; m;
    m.insert(std::pair&lt;RangeKey, int&gt;(RangeKey(1, 2), 1));
    auto it = m.find(RangeKey(1, 2));
  
    std::cout &lt;&lt; it-&gt;first.start &lt;&lt; "\n";
    std::cout &lt;&lt; it-&gt;first.end &lt;&lt; "\n";
    std::cout &lt;&lt; it-&gt;second &lt;&lt; "\n";
  
    return 0;
  }
  </code></pre>
  
  <p>The idea is to consider two RangeKey instances as equal if their ranges overlap. However when I try to retrieve a value after the insertion it gives me some garbage values as the main function output. What am I doing wrong here?</p>