2

This is a more specific question based on a question I asked earlier...

If I have a function that takes two parameters (one required, one optional):

  1. an STL container such as a vector
  2. an "optional" comparison function that serves as a relational overload and returns the maximum value, whatever that is, in the vector...

Code:

template <typename Type>
Type FindMax (std::vector<Type> &myVec, int (*cmp)(Type one, Type two) = CallBack)
/.../

WHAT exactly, does the "int (*cmp)(Type one...)" say to the compiler? I want it to say, here's a function to use when comparing two of type Type...ie when using the relational operators <, >, =, etc. If no function is supplied by the user then use the default, otherwise, use what the user provides...

What exactly does the (*cmp)(Type one, Type two) say? Here's a pointer to a function that takes two parameters Type one and Type two? Is there any significance as to what comes after the *, ie could I write (*titsmagee)(Type one, Type two)? I'm assuming the naming convention is to help future readers?

For this to work with a "struct" does anything specific to the potential comparisons to be made need to be stored within said struct?

Thanks!

1 Answer 1

5
int (*cmp)(Type one, Type two)

The parameter named cmp is a pointer to a function returning int that takes two parameters of type Type.

struct Foo
{
   int x;
};
int compare(Foo x, Foo y)
{
   return (x.x == y.x) ? 0 : (x.x > y.x ? 1 : -1);
}

std::vector<Foo> vec;

FindMax<Foo>(vec, &compare);

You need cmp so you can call the comparison function inside FindMax:

template <typename Type>
Type FindMax (std::vector<Type> &myVec, int (*cmp)(Type one, Type two) = CallBack)
{
    //whatever loop
    max = cmp(myVec[i],myVec[j]) >= 0 ? myVec[i] : myVec[j];
}

EDIT Breaking down the return:

return (x.x == y.x) ? 0 : (x.x > y.x ? 1 : -1);

?: is the ternary conditional operator.

condition ? expression1 : expression2

returns (loosely speaking) expression1 if condition is true, expression2 otherwise.

So what that means is:

if (x.x == y.x) 
   return 0; 
else 
   if (x.x > y.x) 
       return 1; 
   else 
       return -1;

It's what you expect the comparison function to do. Return 0 for equality, 1 if the first element is bigger than the second, and -1 for the inverse.

EDIT 2

struct Foo
{
   int x;
};
//Foo has a member x.
Foo f;
//Create a Foo object called f.
f.x;
//Access the member x of the object
Sign up to request clarification or add additional context in comments.

3 Comments

that makes sense and I can see partly where I've gone wrong. I'm not familiar with the way you wrote a few things. Could you clarify or at least tell me the name of the syntax so that I can read about it? Particularly: "return(x.x == y.x)" How does the x.x, y.x work? What does the "? 0 :" mean?... Basically, if you could break down that "return" line that would be HUGE. Thanks!
Thanks for the edit. That helps a ton. Can someone explain what the "x.x and y.x" is?
damnit, can't believe I didn't see that. You're right, basic. It was that both the instance and the variable were the same name...it threw me. I was thinking, "why is the same variable getting called twice?!" Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.