compare function for upper_bound / lower_bound

前端 未结 4 470
粉色の甜心
粉色の甜心 2020-12-10 03:04

I want to find the first item in a sorted vector that has a field less than some value x.
I need to supply a compare function that compares \'x\' with the internal value

4条回答
  •  [愿得一人]
    2020-12-10 04:06

    Pass a lambda function to upper_bound

     float x;
     MyClass target;
     target.x_ = x;
     std::vector< MyClass >::iterator last = 
     std::upper_bound(myClass.begin(),myClass.end(),target, 
     [](const MyClass& a, const MyClass& b){return a.x_ < b.x_;});
    

提交回复
热议问题