combining 3 functors into 1

hi,
I have 3 functors and was wondering if these can be combined into 1, perhaps as a template.
is it possible? if so, how would I do it. thx!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
struct less_than
{
  bool operator()(double prev,double curr) const
  {
    return prev<curr;
  }
};

struct great_than
{
  bool operator()(double prev,double curr) const
  {
    return prev>curr;
  }
};

struct equal_to
{
  bool operator()(double prev, double curr) const
  {
    return prev==curr;
  }
Last edited on
Maybe, but you'll have to fix the last function (equal_to), you are doing assigment (=) not comparison (==).
Last edited on
thx kooth, I will fix that.
you have 3 structures not functions. Are you trying to experiment with overloading?
well, all i need is really something like the STL functor equal_to. but I want to be able to make it a generic comparison functor, so I can use it to compare 2 values whether they are equal_to, less_than or greater_than.
almost like comparing a time series....if x>y then do A, if x<y then do B, else do C.
how can I modify the below to include less_than and greater_than:
1
2
3
4
5
6
7
template<class T> struct compare:binary_function<T,T,bool>
{
  bool operator()(const T& prev,const T& curr) const
  {
    return prev==curr;
  }
};


never mind, I found it....:-)
Last edited on
then i would use an enum to facilitate a trinary result:
enum status {below, equal, greater};
below = 0, equal = 1, greater = 2 in the enum
then you can declare a variable of type "status" and have a function return a type of "status"
for example:

1
2
3
4
5
6
7
8
9
status statusFlag (double dbl1, double dbl2)
{
if (dbl1< dbl2)
return below;
else if (dbl1 > dbl2)
return greater;
else
return equal;
}


then call it like this:

1
2
3
4
5
6
7
8
9
10
11
12
main ()
{
int    flag;
double inNum1, inNum2;
cout << "Enter 2 numbers: ";
cin >> inNum1 >> inNum2;
cout << endl;
flag = statusFlag (inNum1, inNum2);
cout >> endl >> flag >> endl;     // this will output the number of the enum "position" you can then use the enum "position" to output text, using an array perhaps...thats all an enum is...

return 0;
}


then you can use the status of "flag" to do what you need it to do
Last edited on
What you are looking for is called a "combinator" and there are no combinators in the STL, but some implementations provide some (such as SGI's). It is just as easy to write your own to do exactly what you want...

You do know, however, that if a value is <, ==, or > to another, then the functor might as well be an 'always true' functor.

Hope this helps.
You could make a functor which return 0 if equal, -1 if prev < curr and 1 if prev > curr
thx Bin on the enum, I guess I could use that as well.
thx Duoas, but I want it to return something like aquaz is mentioning.
thx aquaz, let me try that out :-)
shouldn't this work?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

struct compare:public binary_function<double, double, bool>
{
  bool operator()(const double curr, const double prev) const
  {
    if (curr>prev)
      { return 1;}
    else
      {
	if(curr<prev){ return -1;}
      else
	{return 0;}
      }
  }
};

but it is returning 1 when (curr<prev). it is working ok for (curr>prev) and (curr==prev)...where's my error?
is the error because of bool returning only true or false?
Last edited on
yes, your functor must return an int.
Here's some things for you to try:

1
2
3
4
5
6
7
8
9
struct compare: std::binary_function <double, double, int>
{
  int operator () ( double a, double b ) const
  {
    if (a <  b) return -1;
    if (a == b) return  0;
    return 1;
  }
};
1
2
3
4
5
6
7
8
9
10
template <typename T>
struct compare: std::binary_function <T, T, int>
{
  int operator () ( const T& a, const T& b ) const
  {
    if (a <  b) return -1;
    if (a == b) return  0;
    return 1;
  }
};

:-)

[edit] Yoinks! Fixed typo...
Last edited on
thanks Duoas, both of them worked :-)
Topic archived. No new replies allowed.