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 6809729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:07:14+00:00 2026-05-26T20:07:14+00:00

I’m templatizing a queue class so I can use it with anything from ints

  • 0

I’m templatizing a queue class so I can use it with anything from ints to whatever structs I need to define.

I need to pass a comparison function to the class constructor, a predefined comparison function for ints and alike then leave it up to the client to provide any comparison functions they may want. But how do I do this?

template<typename Type>
int cmpFn(Type one, Type two)
{
    if (one < two) return -1;
    if (one > two) return 1;
    return 0;
}

template <typename Type>
class Queue
{
    public:
        Queue()
        {
            Type *list = new Type[size];
            // What do I do now?
            // How to define this constructor?
            // It must pass a comparison function
            // to a private sort method in this class.
        }
    private:
        void sortFunc(Type list, int(fn)(Type one, Type  two)=cmpFn);
};

There’s probably some mistakes in the above code since I just wrote it down from the top of my head to make my question more clear. But all I’m interested in is how to pass a comparison function to a sort method when defining a class.

This is a personal exercise, I’m not enrolled in any course nor have I access to any tutors. I’ve been googling this for a while now, but I couldn’t come up with the right answer… I guess I wasn’t asking the right question to Mr. Google.

P.S.
The client may want to provide comparison functions for any sort of data, like:

struct individual
{
    string name;
    int age;
    double height;
};

I guess that the constructor has to be like this:

Queue(int (*fn)(Type, Type) = cmpFn);

But how do I define/implement this? It’s not a Queue object itself who will use this callback function, but its method: sort();

  • 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-26T20:07:15+00:00Added an answer on May 26, 2026 at 8:07 pm

    Here’s a working, compilable example of what I think you want:

    #include <cstddef>
    #include <string>
    #include <iostream>
    
    template<typename Type>
    int cmpFn(Type one, Type two)
    {
        if (one < two) return -1;
        if (one > two) return 1;
        return 0;
    }
    
    template <typename Type>
    class Queue
    {
        public:
            // This is the typedef for your callback type
            typedef int (*callback)(Type one, Type two);
    
            Queue(Type *list, size_t size, callback func = cmpFn)
            {
                sortFunc(list, size, func); // works too
            }
    
        private:
            void sortFunc(Type *list, size_t size, callback func) {
                for (size_t i=0; i<size; i++) {
                    for (size_t j=0; j<size; j++) {
                        if (i == j) continue;
    
                        int val = (*func)(list[i], list[j]);
                        switch (val) {
                            case 1:
                                std::cout << list[i] << " is greater than " << list[j] << "\n";
                                break;
                            case -1:
                                std::cout << list[j] << " is greater than " << list[i] << "\n";
                                break;
                            case 0:
                                std::cout << list[i] << " and " << list[j] << " are equal\n";
                                break;
                        }
                    }
                }
            }
    
    };
    
    int stringCmp(std::string one, std::string two) {
        if (one.size() < two.size()) return -1;
        if (one.size() > two.size()) return 1;
        return 0;
    }
    
    int main() {
        // compare ints, use generic comparison function (cmpFn)
        int myInts[2] = {1, 2};
        Queue<int> qi(myInts, 2);
    
        // compare strings, use our own comparison function (stringCmp)
        std::string myStrings[2] = {"foo", "bar"};
        Queue<std::string> qs(myStrings, 2, stringCmp);
    
        return 0;
    }
    

    Compiling and executing the program above should give you this output:

    2 is greater than 1
    2 is greater than 1
    foo and bar are equal
    bar and foo are equal
    

    Basically what it does:

    • The Queue constructor accepts a list array, its size and a callback function.
    • If the callback function is not provided, it uses the generic one (cmpFn).
    • It then calls sortFunc which loops though all the elements in the list array and compare them using the callback function.

    In the code above, you have an example with int and std::string.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
Seemingly simple, but I cannot find anything relevant on the web. What is the
I want use html5's new tag to play a wav file (currently only supported
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but

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.