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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:44:33+00:00 2026-06-02T05:44:33+00:00

I have been stuck on this problem for many hours, but I have a

  • 0

I have been stuck on this problem for many hours, but I have a feeling there is a trivial solution that I am not seeing. I am trying to use a priority_queue to create a MinHeap of pointers to a struct I defined. My issue lies in having to overload the > operator for the pointer to this struct (which I know is not possible) in order to match the priority_queue template. This is my last resort attempt to use the stl priority_queue before I decided to ditch it and write my own heap code.

The relevant snippets of my code are: (i) part the struct definition:

typedef struct Node Node;
struct Node
{
    int frequency;
    bool operator>( const Node& other ) const{
        return frequency > other.frequency;
    }
};

(ii) priority queue initialization:

priority_queue<Node*,vector<Node*>,greater<Node*> > q;

And (iii) the for loop constructing the initial heap (assume int_array has been initialized):

for (int i = 0; i < SIZEOFINTARRAY; i++)
{
    Node *n = new Node;
    n->frequency = int_array[i];
    q.push(n);
}

Currently, the elements in this “heap” are returned FIFO, and no sorting happens. I assume this is because the priority comparison checks the pointer and elements earlier in an array are located lower in memory. Please give me any tips on how I could accomplish this.

PS. Sorry if this post does not adhere to stackoverflow standards (I tried my best to follow the rules but it is my first post). I welcome all criticism so I can never make the same mistakes again.

  • 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-02T05:44:36+00:00Added an answer on June 2, 2026 at 5:44 am

    a generic way would be to implement a sort of deref_greater which is just like std::greater but dereferences the input args first.

    template<class T>
    struct deref_greater : public std::binary_function<T, T, bool>
    {
      bool operator()( const T& _Left, const T& _Right ) const
      {
        return *_Left > *_Right;
      }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hope to get solution to this problem. I have been stuck on it since
I have been stuck for a long time on this simple but obtuse message
i know this is basic but somehow i have been stuck here for some
I hope this isn't too vague, but I'm stuck on a problem that has
I have a problem which has been torturing for many hours. I need to
Sorry for the long question, but I have been stuck on this for days
i've been stuck with this problem for like a day, not finding any relevant
I have been stuck on this for days, and was wondering if anyone had
I have been stuck on this for a while. i have tried and I
I have been stuck on this and don't really know how to go about

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.