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:
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:
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
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.
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 :-)
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?
struct compare: std::binary_function <double, double, int>
{
intoperator () ( 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>
{
intoperator () ( const T& a, const T& b ) const
{
if (a < b) return -1;
if (a == b) return 0;
return 1;
}
};