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

  • Home
  • SEARCH
  • 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 8877681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:31:26+00:00 2026-06-14T19:31:26+00:00

I have a class Node that, in addition to storing data, has a pointer

  • 0

I have a class Node that, in addition to storing data, has a pointer to its parent Node. I store some Nodes in a priority_queue and overrode the < operator for comparison.

class Node {
public:
    string name;
    Node *parent;
    int cost;
};

static bool operator<(const Node& lhs, const Node& rhs) {
    return lhs.cost < rhs.cost;
}
priority_queue<Node> queue;

The problem is, the parent pointers seem to be messed up. My guess is, that when I pop a Node from the queue, the Nodes are actually moved up in memory and the pointers point to wrong Nodes. Could this be?

I tried to use a priority_queue of Node* pointers instead (and create them with new), so that only the pointers are reordered, not the objects themselves. This seems to fix the pointer issue, however now the queue is ordered by memory adress, and not the cost of the Nodes.

How can I implement a priority_queue that has objects pointing to each other?

  • 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-14T19:31:28+00:00Added an answer on June 14, 2026 at 7:31 pm

    Use Node* and a custom comparator for your queue. With what you currently have, you can do the following, though i suggest you use smart pointers if they’re available in your current toolchain.

    class Node {
    public:
        string name;
        Node *parent;
        int cost;
    };
    
    class CompareNodePtr
    {
    public:
        bool operator ()(const Node*& left, const Node*& right) const
        {
            return left->cost < right->cost;
        }
    };
    
    // your priority queue with custom comparator.
    std::priority_queue<Node*, std::vector<Node*>, CompareNodePtr> myqueue;
    

    Alternatively, for a more local-defintiion of the comparator, you can stuff the compare-my-pointer class within Node so as not to pollute the global namespace:

    class Node {
    public:
        string name;
        Node *parent;
        int cost;
    
        struct ComparePtr
        {
            bool operator ()(const Node*& left, const Node*& right) const
            {
                return left->cost < right->cost;
            }
        };
    };
    
    // your priority queue with custom comparator.
    std::priority_queue<Node*, std::vector<Node*>, Node::ComparePtr> myqueue;
    

    Regarding pointers being “messed up”, with object-level storage (as opposed to pointer-storage as above) your priority queue can/will move elements around each time it re-heapifies the content after a pop (in case you weren’t aware, the standard lib uses a heap structure for a priority queue by default).

    Finally, I leave the notion of how to deal with a parent-pointer of some node being popped prior to any/some of its children, being deleted, and thereby creating an invalid pointer to any children left in the queue to you, but it sounds like you’re aware that can happen.

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

Sidebar

Related Questions

I have a Node class that has an 'element' slot which contains a list
I have some custom JTree. That tree has nodes with custom icons. I also
I have a class LinkedList clas that has a class called Node class Node
Let me explain best with an example. Say you have node class that can
I have a binary tree class that is created with a root node and
I have a class: class node { public: node& parent; } I want to
I have a class like the following: class node { public: node* parent; std::list<node*>
I have this code for BinaryTree creation and traversal class Node { Integer data;
To keep things simple, lets say I have a Node class, each node has
I have a class (Node) which has a property of SubNodes which is 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.