Thread: Priority Queue with Classes

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    27

    Priority Queue with Classes

    Hi,

    I want to set up a priority queue for my class DeformationCandidate and sets the priority based on the value of the density variable. I am having a rough time determining the syntax for the queue and am not sure how I need to overload the ==, <, > operators.

    DeformationCandidate.h:
    Code:
    class DeformationCandidate
    {
    public:
    	DeformationCandidate();
    	~DeformationCandidate(){};
    
    	//The row number of the candidate point
    	int row;
    
    	//The column number of the candidate point
    	int column;
    
    	//The tissue density of the candidate point
    	int density;
    
    private:
    
    };
    DeformationCandidate.cpp:
    Code:
    # include "DeformationCandidate.h"
    
    DeformationCandidate::DeformationCandidate()
    {
    	//Initialize variable values
    	row = -1;
    	column = -1;
    	density = -1;
    
    }
    First, how do I declare the queue? Is it:
    Code:
    std::priority_queue<DeformationCandidate> _deformationQueue;
    Second, what do I need to do for the operators?

    I'm having a hard time finding a good resource for priority queues, so please let me know if you have any recommended resources.

    Thanks!
    JackR

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by JackR
    First, how do I declare the queue?
    Yes, that is correct. However, avoiding prepending an underscore to your variable names. Names that begin with an underscore followed by a lowercase character are reserved to the (language and standard library) implementation for use in the global namespace. Names that begin with an underscore followed by an uppercase character, or that contain two consecutive underscores, are reserved to the implementation for any use.

    Quote Originally Posted by JackR
    Second, what do I need to do for the operators?
    Unless you want to provide a custom comparator, you need to overload operator< for DeformationCandidate in order to store DeformationCandidate objects in a std:riority_queue<DeformationCandidate>. It would probably also make sense to overload operator== in that case.

    You probably should declare the member variables private and provide suitable member functions to form a minimal but complete interface for DeformationCandidate. If you actually want to initialise rather than assign to the member variables, use the constructor's initialisation list, e.g.,
    Code:
    DeformationCandidate::DeformationCandidate() : row(-1), column(-1), density(-1) {}
    Quote Originally Posted by JackR
    I'm having a hard time finding a good resource for priority queues, so please let me know if you have any recommended resources.
    Refer to this Standard Template Library Programmer's Guide and this C++ Reference.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  2. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 03:55 AM
  3. Priority Queue Help
    By cjwenigma in forum C++ Programming
    Replies: 6
    Last Post: 11-15-2007, 12:48 AM
  4. help with queues
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-21-2002, 09:09 PM
  5. help with queues
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-21-2002, 11:39 AM