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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:19:54+00:00 2026-06-16T01:19:54+00:00

I want to be able to create a compare function using a class, eg:

  • 0

I want to be able to create a compare function using a class, eg:

bool someClassLess(const someClass &a1, const someClass &a2) {
    return (a1.variable1 < a2.variable1 && a1.variable2 < a2.variable2);
}

Then declare a priority queue of someClass and pass my compare function to be used when pushing elements, eg:

PriorityQueue<someClass> arr(someClassLess());

If no compare function is passed when declaring the priority queue, it should automatically use the less function in functional library when pushing.

How do I pass a compare function to a class?

Below you can find code of my selfwritten PriorityQueue. The code includes a failed attempt to pass a compare function.

#ifndef PRIORITY_QUEUE_H
#define PRIORITY_QUEUE_H

#include <iostream>
#include <functional>

using std::cout;
using std::endl;
using std::less;

template<typename T>
class PriorityQueue {
public:

    template<typename PRED>
    PriorityQueue(PRED compare);
    ~PriorityQueue();

    T pop();
    void push(const T &e);
    size_t getSize() const;
    bool isEmpty() const;
    void print() const;

private:

    T *end;
    T *queue;
    PRED compare;

};

template<typename T>
PriorityQueue<T>::PriorityQueue(PRED compare = less<T>()) : queue(0), end(0), compare(compare) {
}


template<typename T>
PriorityQueue<T>::~PriorityQueue() {
    delete [] queue;
}

template<typename T>
T PriorityQueue<T>::pop() {
    if(isEmpty()) {
        throw "Queue is empty";
    } else if(getSize() == 1) {
        T removed = *queue;
        delete [] queue;
        queue = end = 0;
        return removed;
    }

    T *newQueue = new T[getSize() - 1];
    // Iteratorer
    T *it = queue, *itNew = newQueue;

    T removed = *(it++);
    for( ; it != end; it++, itNew++) {
        *itNew = *it;
    }

    int oldSize = getSize();

    T *tmp = queue;
    queue = newQueue;
    delete [] tmp;

    end = queue + oldSize - 1;

    return removed;
}

template<typename T>
void PriorityQueue<T>::push(const T &e) {
    if (isEmpty()) {
        queue = new T[1];
        *queue = e;
        end = queue + 1;
        return;
    }

    T *newQueue = new T[getSize() + 1];
    // Iterators
    T *it = queue, *itNew = newQueue;

    // Find where element e should be inserted, whilst inserting elements
    // compare(*it, e) used to look like *it < e when I was initially creating the class
    for( ; compare(*it, e) && it != end; it++, itNew++) {
        *itNew = *it; 
    }

    // Insert e
    *(itNew++) = e;

    // Insert the remaining elements
    for ( ; it != end; it++, itNew++) {
        *itNew = *it;
    }

    int oldSize = getSize();

    T *tmp = queue;
    queue = newQueue;
    delete [] tmp;

    end = queue + oldSize + 1;
}

template<typename T>
size_t PriorityQueue<T>::getSize() const {
    return (end - queue);
}

template<typename T>
bool PriorityQueue<T>::isEmpty() const {
    return (getSize() <= 0);
}

template<typename T>
void PriorityQueue<T>::print() const {
    for(int *i = queue; i != end; i++) {
        cout << *i << endl;
    }
}

#endif
  • 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-06-16T01:19:56+00:00Added an answer on June 16, 2026 at 1:19 am

    Why not just shamelessly copy the implementation of std::priority_queue. Just use this:

    template <typename T, typename Compare = std::less< T > >
    class PriorityQueue {
      public:
        PriorityQueue(Compare comp = Compare()) : end(0), queue(0), compare(comp) { };
    
        // ...
    
      private:
    
        T *end;
        T *queue;
        Compare compare;
    
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to be able to create modal dialogs, with, for example close: function()
I want to be able to create the url: http://example.com/+/user Is it possible using
I want to be able to create a Pandas DataFrame with MultiIndexes for the
I want to be able to create links looking like buttons with dynamic text
I want to be able to create a temporary text File in Java to
I want to be able to create the XSD file for my typed dataset
I want to be able to create a KPI List on my MOSS 2007
I want to be able to create a file from the Content Provider, however
hey i want to be able to create multiple shapes and store them perhaps
We want people to be able to create a thumbnail version of an uploaded

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.