C++ 上界/下界的比较函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/869593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:40:13  来源:igfitidea点击:

compare function for upper_bound / lower_bound

c++algorithmstl

提问by Martin Beckett

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 in MyClass but I can't work out the function declaration.
Can't I simply overload '<' but how do I do this when the args are '&MyClass' and 'float' ?

我想在一个字段小于某个值 x 的排序向量中找到第一项。
我需要提供一个比较函数,将“x”与 MyClass 中的内部值进行比较,但我无法计算出函数声明。
我不能简单地重载 '<' 但是当参数是 '&MyClass' 和 'float' 时我该怎么做?

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

回答by Mark Ransom

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.

正如你所看到的,这是很长的路要走。

回答by Ghostrider

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

这使它更具可读性

回答by Borbus

I think what you need is std::bind2nd(std::less<MyClass>(), x). But, of course, the operator< must be defined for MyClass.

我认为你需要的是std::bind2nd(std::less<MyClass>(), x). 但是,当然,必须为 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. However, there might be a better way to do this.

编辑:哦,我认为您需要一个 MyClass 的构造函数,它只接受一个浮点数,以便可以隐式转换。但是,可能有更好的方法来做到这一点。