title: Tolerant key lookup in std::map
body: <p>Requirements:</p>
  
  <blockquote>
    <ol>
    <li>container which sorts itself based on numerically comparing the keys (e.g. std::map)</li>
    <li>check existence of key based on float tolerance (e.g. map.find() and use custom comparator )</li>
    <li>and the tricky one: the float tolerance used by the comparator may be changed by the user at runtime!</li>
    </ol>
  </blockquote>
  
  <p>The first 2 can be accomplished using a map with a custom comparator:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>struct floatCompare : public std::binary_function&lt;float,float,bool&gt;
  {
      bool operator()( const float &amp;left, const float &amp;right ) const
      {
          return (fabs(left - right) &gt; 1e-3) &amp;&amp; (left &lt; right);
      }
  };
  
  typedef std::map&lt; float, float, floatCompare &gt; floatMap;
  </code></pre>
  
  <p>Using this implementation, floatMap.find( 15.0001 ) will find 15.0 in the map.</p>
  
  <p>However, let's say the user doesn't want a float tolerance of 1e-3.
  What is the easiest way to make this comparator function use a variable tolerance at runtime?  I don't mind re-creating and re-sorting the map based on the new comparator each time epsilon is updated.</p>
  
  <p>Other posts on modification after initialization <a href="https://stackoverflow.com/questions/13935241/stdmap-change-key-comp-after-initialization">here</a> and using floats as keys <a href="https://stackoverflow.com/questions/6684573/floating-point-keys-in-stdmap">here</a> didn't provide a complete solution.</p>