Re: asymmetric functor predicates?
On Apr 3, 7:15 pm, Stephen Howe <sjhoweATdialDOTpipexDOTcom> wrote:
Is it possible to use asymmetric functor predicates?
Yes, but it depends on the context.
For example you could have
#include <algorithm>
#include <vector>
struct point2D
{
int x;
int y;
};
class CCompare2D
{
bool operator()(const point2D &e1, const const point2D &e2) const
I'm not sure what you were trying to do, but the above
declaration is illegal. All it does is duplicate a const in the
second parameter.
{
return ((e1.c != e2.x) ? (e1.x < e2.x) : (e1.y < e2.y));
}
};
:
:
vector <point2D> v2D;
:
// populate v2D
:
sort (v2D.begin(), v2D.end(), CCompare2D());
Now if you had
bool DoesXExist(int x)
{
return binary_search(v2D.begin(), v2D.end(), x, SomeFunctor());
}
I can easily provide some conversion constructor that turns x
into a point2D And then I can provide another functor that
only processes just the x portion of point2D
But what I would like to do is get away from having conversion
constructors at all. I presume I would have some functor that
is like so
class SomeFunctor
{
bool operator()(const point2D &e1, int x) const
{
return (e1.x < x);
}
bool operator()(int x, const point &e1) const
{
return (x < e1.x);
}
};
Am I on the right lines?
Yes. The standard requires that comp(*j, value) be called in
lower_bound, and comp(value, *j) in upper_bound; in theory, at
least, you'd only have to provide one of the overloads for
either of these functions. binary_search requires both,
however, and I'd probably generally provide both, just to be on
the safe side.
--
James Kanze