Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7621037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:05:46+00:00 2026-05-31T04:05:46+00:00

This is a more specific question based on a question I asked earlier… If

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T04:05:48+00:00Added an answer on May 31, 2026 at 4:05 am
    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a question related to another one I asked that was specific to
I know that this question might have been asked like 100 times, but, believe
This is a more HTML based question then iOS -specific but how would you
I'm reviving this question, and making it more specific: Is there a .NET framework
I noticed This question , but my question is a bit more specific. Is
This is kind of more generic question, isn't language-specific. More about idea and algorithm
I know that the question has been asked before , but it's been two
This question was asked already here , but rather than answering the specific question,
I asked the question this way because I can imagine that there's a potentially
This isn't really S3-specific, it's more just a basic JavaScript question. Is there a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.