Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compare function for upper_bound / lower_bound

Tags:

c++

algorithm

stl

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' ?

 float x;
 std::vector< MyClass >::iterator last = std::upper_bound(myClass.begin(),myClass.end(),x);
like image 842
Martin Beckett Avatar asked May 15 '09 16:05

Martin Beckett


People also ask

What does the lower_bound function do in C++?

lower_bound in C++ The lower_bound() method in C++ is used to return an iterator pointing to the first element in the range [first, last) which has a value not less than val. This means that the function returns an iterator pointing to the next smallest number just greater than or equal to that number.

What does upper_bound function do in C++?

upper_bound() is a standard library function in C++ defined in the header . It returns an iterator pointing to the first element in the range [first, last) that is greater than value, or last if no such element is found. The elements in the range shall already be sorted or at least partitioned with respect to val.

What will the lower_bound () function return if the element you passed as an argument isn't present in the vector VECT?

lower_bound returns an iterator pointing to the first element in the range [first,last) which has a value not less than 'val'. and if the value is not present in the vector then it returns the end iterator.

What is the difference between upper bound and lower bound in C++?

Upper bound and Lower bound for non increasing vector in C++ In a Vector, lower bound returns an iterator pointing to the first element in the range that does not compare the given value. Upper Bound returns an iterator pointing element in the range that smaller than given value.


2 Answers

What function did you pass to the sort algorithm? You should be able to use the same one for upper_bound and 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() :

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.

like image 182
Mark Ransom Avatar answered Sep 20 '22 16:09

Mark Ransom


You can further improve Mark's solution by creating a static instance of MyClassLessThan in MyClass

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

This way you can call lower_bound in the following way:

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

This makes it a bit more readable

like image 28
Ghostrider Avatar answered Sep 21 '22 16:09

Ghostrider