简体   繁体   English

上界/下界的比较函数

[英]compare function for upper_bound / lower_bound

I want to find the first item in a sorted vector that has a field less than some value x.我想在一个字段小于某个值 x 的排序向量中找到第一项。
I need to supply a compare function that compares 'x' with the internal value in MyClass but I can't work out the function declaration.我需要提供一个比较函数,将“x”与 MyClass 中的内部值进行比较,但我无法计算出函数声明。
Can't I simply overload '<' but how do I do this when the args are '&MyClass' and 'float' ?我不能简单地重载 '<' 但是当参数是 '&MyClass' 和 'float' 时我该怎么做?

 float x;
 std::vector< MyClass >::iterator last = std::upper_bound(myClass.begin(),myClass.end(),x);

What function did you pass to the sort algorithm?你传递给排序算法的函数是什么? You should be able to use the same one for upper_bound and lower_bound.您应该能够对 upper_bound 和 lower_bound 使用相同的一个。

The easiest way to make the comparison work is to create a dummy object with the key field set to your search value.进行比较的最简单方法是创建一个虚拟对象,并将键字段设置为您的搜索值。 Then the comparison will always be between like objects.那么比较将始终在相似的对象之间进行。

Edit: If for some reason you can't obtain a dummy object with the proper comparison value, then you can create a comparison functor.编辑:如果由于某种原因您无法获得具有正确比较值的虚拟对象,那么您可以创建一个比较函子。 The functor can provide three overloads for operator() :函子可以为 operator() 提供三个重载:

struct MyClassLessThan
{
    bool operator() (const MyClass & left, const MyClass & right)
    {
        return left.key < right.key;
    }
    bool operator() (const MyClass & left, float right)
    {
        return left.key < right;
    }
    bool operator() (float left, const MyClass & right)
    {
        return left < right.key;
    }
};

As you can see, that's the long way to go about it.正如你所看到的,这是很长的路要走。

You can further improve Mark's solution by creating a static instance of MyClassLessThan in MyClass您可以通过在 MyClass 中创建 MyClassLessThan 的静态实例来进一步改进 Mark 的解决方案

class CMyClass 
{
   static struct _CompareFloatField
   {
      bool operator() (const MyClass & left, float right) //...
      // ...
   } CompareFloatField;
};

This way you can call lower_bound in the following way:这样你就可以通过以下方式调用lower_bound:

std::lower_bound(coll.begin(), coll.end(), target, CMyClass::CompareFloatField);

This makes it a bit more readable这使它更具可读性

Pass a lambda function to upper_bound将 lambda 函数传递给 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_;});

I think what you need is std::bind2nd(std::less<MyClass>(), x) .我认为你需要的是std::bind2nd(std::less<MyClass>(), x) But, of course, the operator< must be defined for MyClass.但是,当然,必须为 MyClass 定义 operator<。

Edit: oh and I think you will need a constructor for MyClass that accepts only a float so that it can be implicitly converted.编辑:哦,我认为您需要一个 MyClass 的构造函数,它只接受一个浮点数,以便可以隐式转换。 However, there might be a better way to do this.但是,可能有更好的方法来做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM