title: How to find max element of a vector with user defined predicate
body: <p>I need to find the maximum element of a vector. I want to use max_element from the <a href="https://en.wikipedia.org/wiki/Standard_Template_Library" rel="nofollow noreferrer">STL</a> library.</p>
  <p>The code I have tried is:</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;algorithm&gt;
  
  using namespace std;
  
  class A
  {
  public:
      A(int _size, long _len) : size(_size), len(_len){}
  
      int size;
      long len;
  };
  
  bool CompareMaxA(const A&amp; _a, const A&amp; _b)
  {
      return _a.len &gt; _b.len;
  }
  
  int main() {
  
      vector&lt;A&gt; vec;
      A a1(3, 10);
      A a2(5, 30);
      vec.push_back(a1);
      vec.push_back(a2);
  
      auto it = max_element(vec.begin(), vec.end(), CompareMaxA);
  
      cout &lt;&lt; &quot;max = &quot; &lt;&lt; it-&gt;len;
  
      return 0;
  }
  </code></pre>
  <p>I get max = 10, instead of max = 30. Why?</p>