title: C++ lambdas for std::sort and std::lower_bound/equal_range on a struct element in a sorted vector of structs
body: <p>I have a std::vector of this struct:</p>
  
  <pre><code>struct MS
  {        
    double aT;
    double bT;
    double cT;
  };
  </code></pre>
  
  <p>which I want to use std::sort on aswell as std::lower_bound/equal_range etc...</p>
  
  <p>I need to be able to sort it and look it up on either of the first two elements of the struct. So at the moment I have this:</p>
  
  <pre><code>class MSaTLess 
  {
  public:
    bool operator() (const MS &amp;lhs, const MS &amp;rhs) const
    {
      return TLess(lhs.aT, rhs.aT);
    }
    bool operator() (const MS &amp;lhs, const double d) const
    {
      return TLess(lhs.aT, d);
    }
    bool operator() (const double d, const MS &amp;rhs) const
    {
      return TLess(d, rhs.aT);
    }
  private:
    bool TLess(const double&amp; d1, const double&amp; d2) const
    {
      return d1 &lt; d2;
    }
  };
  
  
  class MSbTLess 
  {
  public:
    bool operator() (const MS &amp;lhs, const MS &amp;rhs) const
    {
      return TLess(lhs.bT, rhs.bT);
    }
    bool operator() (const MS &amp;lhs, const double d) const
    {
      return TLess(lhs.bT, d);
    }
    bool operator() (const double d, const MS &amp;rhs) const
    {
      return TLess(d, rhs.bT);
    }
  private:
    bool TLess(const double&amp; d1, const double&amp; d2) const
    {
      return d1 &lt; d2;
    }
  };
  </code></pre>
  
  <p>This allows me to call both std::sort and std::lower_bound with MSaTLess() to sort/lookup based on the aT element and with MSbTLess() to sort/lookup based on the bT element.</p>
  
  <p>I'd like to get away from the functors and use C++0x lambdas instead. For sort that is relatively straightforward as the lambda will take two objects of type MS as arguments.</p>
  
  <p>What about for the lower_bound and other binary search lookup algorithms though? They need to be able to call a comparator with (MS, double) arguments and also the reverse, (double, MS), right? How can I best provide these with a lambda in a call to lower_bound? I know I could create an MS dummy object with the required key value being searched for and then use the same lambda as with std::sort but is there a way to do it without using dummy objects?</p>