title: How can I find the minimum value in a map?
body: <p>I have a <code>map</code> and I want to find the minimum value (right-hand side) in the map. Here is how I did it:</p>
  
  <pre><code>bool compare(std::pair&lt;std::string ,int&gt; i, pair&lt;std::string, int&gt; j) {
    return i.second &lt; j.second;
  }
  ////////////////////////////////////////////////////
  std::map&lt;std::string, int&gt; mymap;
  
  mymap["key1"] = 50;
  mymap["key2"] = 20;
  mymap["key3"] = 100;
  
  std::pair&lt;char, int&gt; min = *min_element(mymap.begin(), mymap.end(), compare); 
  std::cout &lt;&lt; "min " &lt;&lt; min.second&lt;&lt; " " &lt;&lt; std::endl;
  </code></pre>
  
  <p>The code above works fine and I'm able to get the minimum value. However, when I put this code inside my class as follows, it doesn't seem to work:</p>
  
  <pre><code>int MyClass::getMin(std::map&lt;std::string, int&gt; mymap) {
    std::pair&lt;std::string, int&gt; min = *min_element(mymap.begin(), mymap.end(), 
                                                   (*this).compare);
                                                   // Error probably due to "this".
    return min.second; 
  }
  
  bool MyClass::compare(
      std::pair&lt;std::string, int&gt; i, std::pair&lt;std::string, int&gt; j) { 
    return i.second &lt; j.second; 
  }
  </code></pre>
  
  <p>How can I make the code work with my class? Also, is there a better solution which doesn't require writing the additional <code>compare</code> function?</p>